Update on the use of ngspice from EEschema

I thought it was time to post updates to the things I have noticed while testing the ngspice simulation called directly from within EEschema.
I have posted about this before, but I am updating my notes as I find new quirks.

Spice assumes pins in its models to be:
MOSFET: m[name] [drain] [gate] [source] [substrate]
JFET: j[name] [drain] [gate] [source] [model]
BJT: q[name] [collector] [base] [emitter] [model]
DIODE: d[name] [anode] [cathode]

Many models in KiCAD libs are not designed for use with ngspice
and hence the pin numbers are not in accordance with the assumptions
in ngspice listed above. Even Diodes differ.
Use ‘alternate pin’-field in spice settings. In practice this
means ‘alternate pin’ configurations are used for almost all

Parts that are not relevant for the simulation, like
connectors or heatsinks, must be disabled
for simulation in edit/spice model.
Otherwise results are unpredictable
(like >130kA flowing out of connector
into transistor)

It is possible to use models that are downloaded from manufacturers, but most manufacturers do offer models for pspice. There are slight differences, see below.

Some models are implemented using SUBCKT. In the Spice settings, the ‘Spice primitive’ must be set to ‘X’.

Model or subckt names should not contain ‘-’.
From the ngspice manual: “A name
field must begin with a letter (A through Z) and cannot contain any delimiters.”
In practice, many spice models downloaded from manufacturers, written for pspice,
will have to be adapted to follow the ngspice naming conventions.

Ngspice does not seem to fully follow the rules above. It does allow some delimiters, such as the hyphen, but be aware of the naming conventions and check in case of difficulties.

Comments start with '* ’ so ‘*#’ is not ok. (this is probably a bug in ngspice)
This is another reason why downloaded pspice models need to be adapted before
use with ngspice.

@hermit made the remark that on some part models, the pins are not numbered but named. He used the 7805 voltage regulator as an example.
"The pins in the symbol are in the correct order but they are not numbered. "
The spice model, however, uses pin numbers. For instance (first two lines op 7805 subckt):

.SUBCKT LM7805 1 2 3

  • In GND Out

I have not found an7805 without pin numbers in the recent libraries, but I suppose that cases like this can be solved by using the ‘Alternate node sequence’ field in the Spice settings.

Alternate node sequence IN,GND,OUT

Watch the spice results for unexpected results like enormous currents because they can be a clue that something in the pin ordering is wrong.

@hermit also added the following hint:

One hint you might add is, “Do your plots look upside down?” It was the key to my realizing the diodes were backwards. Took too long. :frowning: I was so used to seeing examples of fully rectified AC as positive peaks I didn’t immediately recognize I had the right graph.

Please feel free to comment or add to this.

N.B.: I am offering a download as an example schematic that illustrates most of the points mentioned above, but note that the libraries are currently under reorganisation. You might need to tweak the library settings a bit to get to all parts in the example.
CurrentsourceKiCAD-NGspice03.zip (7.0 KB)


I fixed a bit of the text above due to new insights.

Ok. I have made a new example. I did use the ‘Alternative node sequence’ route to include an 7805 model as an example of a part that has its pins named, instead of numbered.

However, I found 3 variations of the 7805 in regul.lib (L7805, LM7805-TO220 and uA7805), and all of them have pin numbers.
Which 7805 did you use, @hermit?

Some comments from my side as a ngspice maintainer.

The pin numbering of basic devices (bip, MOS, diodes) in ngspice is fixed and the same for all simulators. This will not change, because any change would break a lot of things, especially the manufacurer’s device models comig as subcircuits.

These subcircuits most often are defined being PSPICE compatible. As mifi described, there are tiny differences making a direct import impossible without some tweaking of the model description.

All comment lines in ngspice may start with '*'. Unfortunately the '*#' case is a special one (see ngspice manual chapt. 17.5.70), that we have kept for backward compatibility.

Concerning connectors: Why not make spice models for connectors (a few milli Ohm series resistance per contact plus some pin-to-pin capacitance)?

Heatsinks are another interesting story. Basically ngspice can also handle thermal simulations. A potential difference (aka voltage) becomes a temperature difference, the electrical resistance a thermal resistance, the current a heat flow and the electrical capacitance a thermal capacitance. If set up properly (this has still to be done), one could simulate the effectiveness of heatsinks.

For some time now I am working on a ngspice branch https://sourceforge.net/p/ngspice/discussion/ngspice-tips/thread/7838169b/ that adds the capability to ngspice reading PSPICE device libraries and doing the necessary translations towards ngspice automatically. Every time a device library is added to the ngspice netlist by the .include command, the translator is invoked, when the spinit or .spiceinit file contains the line ‘set ngbehavior=ps’.

MS Windows users of the KiCad nightly may test this by downloading a pre-build binary from the ngspice download page at http://ngspice.sourceforge.net/download.html . If you check for your KiCad installation, search at Kicad/bin for libngspice-0.dll, rename it to libngspice-0_orig.dll, take ngspice.dll from the download, rename it to libngspice-0.dll and place it into the KiCad/bin directory. Please also copy vcomp140.dll to KiCad/bin, and thats it. LINUX users might download ngspice via git from the PSPICEComp2 branch and compile it as a shared library.

Your comments as KiCad users are welcome, is this a way to go?

1 Like

About pin mapping, this issue over at the github repo might be of interest: https://github.com/KiCad/kicad-symbols/issues/189
(And the original issue in the old repo. https://github.com/KiCad/kicad-library/issues/182)

According to Wayne there is a special field that allows maping from kicad symbol pin numbers to different spice model pin numbers. (More details see the first issue linked above)

There is @Rene_Poschl, I can confirm that. And it is working well.
This is the ‘alternate pin’ field I was talking about in the original post.