To browser these website, it's necessary to store cookies on your computer.
The cookies contain no personal information, they are required for program control.
  the storage of cookies while browsing this website, on Login and Register.

GDPR and DSGVO law

Storing Cookies (See : ) help us to bring you our services at . If you use this website and our services you declare yourself okay with using cookies .More Infos here:
If you do not agree with storing cookies, please LEAVE this website now. From the 25th of May 2018, every existing user has to accept the GDPR agreement at first login. If a user is unwilling to accept the GDPR, he should email us and request to erase his account. Many thanks for your understanding

User Menu

Google Search

Custom Search

Author Topic: Ngspice and gEDA circuit simulation  (Read 5428 times)

Offline ayeaye

  • Hero Member
  • *****
  • Posts: 868
Ngspice and gEDA circuit simulation
« on: September 29, 2015, 02:05:14 PM »
I used these applications for the first time, and share my experience, so that you will not fall into the same pits, when starting to use them.

One may say that ngspice is the today's version of the SPICE circuit simulator, which they created at Berkeley labs in 1973. This was written for mainframe computers, and worked in batch mode, the same as ngspice does today. And yes they still fix bugs in it. It is a very important software and was used a lot for simulating electronics and integrated circuits.

In Linux you simply install the geda package, geda contains ngspace, gschem for drawing circuits and PSB Designer for creating printed circuit boards. I don't know whether there is a version for Windows.

The most counter-intuitive in gschem is perhaps that you can move around the drawing by holding down the middle mouse button, and with the mouse wheel you can resize. The drawing can be made black on white from the view menu. Deselect the component when you don't want to do any more operations on it.

Draw the circuit with gschem. Pick the components from the list of components there, then join them with nets (wires). The components can be rotated and moved, and all their attributes can be rotated and moved. Select a component, right click on it and click on edit, then you can edit the attributes. Mostly you want them to be visible and show only their value. What you mostly only need are the attributes refdes and value. Refdes is the name of the component such as R1, and value is all the other information you want to provide about the component, such as value, model name, subsircuit name or parameters of the sine or pulse voltage source. Both gschem and ngspice understand well the suffixes such as k, mV or uF, after the numbers.

The nets (wires) have an attribute netname. You should add netnames to some of the wires, so that every node has a netname. The netnames are usually n0, n1, etc, but the netname of the ground node always has to be 0.

Don't use the battery symbol, it doesn't work, use a dc voltage source instead. There should be dc 0 in the beginning of the value of the pulse voltage source, at first ngspice yelled at me that, and didn't work. There should be a resistive path to ground from every node, otherwise ngspice complains about a "singular matrix". This can be achieved by adding resistors with very high resistance. If a component has a subsircuit model, such as a mosfet, its nodes have to be in the same order as the terminal nodes of the model, they usually are, and it has to have a refdes starting with X.

Draw the circuit, then save it, it will have an sch extension. Then go to the directory where you saved the circuit, in terminal. There create a netlist from the circuit, with gnetlist, for example gnetlist -g spice -o test.sch . Netlist is where all the information about circuit is. Every simple line consists of refdes, nodes the component is connected to, and value. The netlist has to end with .END. It may also contain models and subsircuits, simply copy them before the components. Models can be added with gschem too, when they are in separate files.

The spice models are mostly provided by the manufacturers. I found a few main sites to find spice models: International Rectifier for mosfets, for diodes, and Motorola provides spice models for bipolar transistors and some other components. What you need is a spice model, not pspice model or other types of models. Not all models you may need are there, so you should find equivalents.

Start ngspice with the netlist as an argument. For dc simulation simply write op. This enables you to print the voltages of the nodes with print, like print n3 - n2, that prints the voltage betweem the nodes n3 and n2. For seeing how the things change, use a transient simulation. For that write tran , followed by the step (time period) and the end time. After the transient simulation you can plot diagrams from the data stored. The arguments of plot are similar to print, every voltage can be expressed as an equation, but better put the expressions in parentheses.

You can also use hardcopy instead of plot, this writes a postscript file instead of the diagram. You may want to write set hcopypscolor=1 before using it, to draw with colors. The postscript files can be then converted to jpg using gimp or with the command line utility convert, if you have imagemagic installed.

Ngspice as a command line program may look intimidating at first, but it is not so difficult when you know some basic things how to use it. It is the most classic of all circuit simulators, and you avoid a lot of commercial hell, also freeware hell, or poor quality gui programs hell. A lot of commercial circuit simulators use the ngpice code, btw, and at that often some outdated version of it.

That's it, hope it was useful for some.
« Last Edit: September 29, 2015, 08:19:22 PM by ayeaye »

Offline ayeaye

  • Hero Member
  • *****
  • Posts: 868
Re: Ngspice and gEDA circuit simulation
« Reply #1 on: October 03, 2015, 09:21:03 PM »
One more caveat, don't use M as a suffix for values. Ngspice recognizes all suffixes such as k, m, u, n, p, but it does not regognize M. Thus the values must be written without the suffix M, or they will be wrong in the simulation.