Adding new device levels to NGspice/KiCAD

Hello All,

Is there a way to create new device levels with the included ngspice? I am looking to create a new device level that has it’s own equations (not a .lib file) and can have altered parameters inside KiCAD. For more clarity, this is similar to the idea of adding a new Verilog-A device into ngspice as done in their example. I could not find any of the ngspice configuration files, and I am also not sure if a full recompile of KiCAD would be required when adding the device level. Does anyone have experience with this or know what directory I could look in to find the configuration files?

I currently have KiCAD 5.1.9.

As I understand it, you are interested in adding for example another MOS device to ngspice, including its own model equations and model parameter set.

This task has nothing to do with KiCad, but would be a pure ngspice effort. Basically it can be done, but will require diving deeply into ngspice coding.

Please check http://ngspice.sourceforge.net/extras.html for further info.

The situation might be different if you consider using a device which is available in ngspice, but is not supported (yet) by the KiCad GUI. Then it will be possible to find a workaround to make it available in KiCad. Please check the ngspice manual for models available (https://sourceforge.net/projects/ngspice/files/ng-spice-rework/34/ngspice-34-manual.pdf/download), chapters 3 - 12.

Adding the device to ngspice is what I am hoping to accomplish. KiCAD is bundled with ngspice-31 though, right? Do you know where I can find the configuration files for that ngspice install? I cannot find the ngspice-31 install location. I have looked on both Ubuntu and Windows.

On my linux box, a simple “locate ngspice” returns (together with some more)

paul@medion:~$ locate ngspice
/usr/lib/x86_64-linux-gnu/libngspice.so.0
/usr/lib/x86_64-linux-gnu/libngspice.so.0.0.0
/usr/share/doc/libngspice0
/usr/share/doc/libngspice0/changelog.Debian.gz
/usr/share/doc/libngspice0/copyright
/var/lib/dpkg/info/libngspice0:amd64.list
/var/lib/dpkg/info/libngspice0:amd64.md5sums
/var/lib/dpkg/info/libngspice0:amd64.shlibs
/var/lib/dpkg/info/libngspice0:amd64.symbols
/var/lib/dpkg/info/libngspice0:amd64.triggers

Thanks for the list. I also have those locations listed (along with other ngspice installs I made separately). Based off that it looks like it uses a shared library to access the files. I am not too versed in shared libraries, but I thought it still needed a local copy of the code for the shared library to access. Can it instead pull the code directly from GitHub or another location? That would seem like a lot of extra work to do on startup.

ngspice is included into KiCad as a shared library libngspice-0.dll (or similar *.so in Linux or *.dylib on macOS). And there are some *.cm files containing code models.

The ngspice website is http://ngspice.sourceforge.net/ and the source code is available from https://sourceforge.net/projects/ngspice/.

And yes you may compile the shared ngspice independently from KiCad and exchange old with new version.

Btw. code models might be a means to add another device to ngspice without need to change the ngspice source code. Please see http://ngspice.sourceforge.net/xspice.html and http://ngspice.sourceforge.net/xspicehowto.html.

Appreciate the help. I will try adding devices to ngspice and adding it as a new shared library to KiCAD. Will update once I get it tested.

Finally got back to adding the xspice models into KiCAD. I noticed they do not appear as part of the symbol list. Where do the models appear? Are they added through a .lib so that they can be attached to a symbol, or do they have to be called directly in the netlist?

Using ngspice-34 and KiCAD 5.1.9. Already updated the shared library links as well with the xspice models added into the ngspice-34 compilation.

The code models are a ngspice entity. Currently they are not actively supported by KiCad. So you have to take care about them yourself.

The code models are not descriptions of readily available ICs, but are circuit blocks to create simple or complex circuits. So there will be no direct support via footprints or designing them into PCBs (the main task of KiCad). But code models migt be used for example to describe the venerable digital series of 74… ICs. These models of course then will support using real devices with footprints etc. This has already been discussed here somewhere, but nobody yet has taken the job.

You may also use KiCad/Eeschema to develop new circuits by simulation and thus use Eeschema as an schematic entry tool for ngspice. Here you might very well use code models, as circuit block with their own symbol per code model, but also combined as a larger circuit under the hood of a more complex symbol.

A typical way to do so would need the following steps:

Put the code model instance (the a… line) and its associated .model line into a ngspice subcircuit.

Save this subcircuit in a file (e.g. mycm.lib).

Create a symbol with appropriate pins. The symbol pins have to comply with the subcircuit nodes (quantity and sequence). The subcircuit nodes have to be connected to the code model nodes inside of the subcircuit.

Place the symbol onto your Eeschema drawing sheet. Link the model (the mycm.lib file) to the symbol via the ‘Edit Spice Model’ page.

You may start with one of the existing code models, e.g. the gain block, which has 2 pins (in and out) only. A subcircuit may look like this (not tested, see ngspice manual 2.4 and 12.2.1):

.subckt gain1 in out
    a1 in out amp
    .model amp gain (in_offset=0.1 gain=5.0 out_offset=-0.01)
.ends

To make use of this gain block, you will need to use (or make) an eeschema symbol with 2 pins (pin 1 for in, pin 2 for out). When you then add a (ngspice) voltage source at the input and a load resistor at the output, you may start your simulations.

Finally the subcircuit may contain your own code models, or a more complex circuit assmbly. The following example is an up-down counter, made from a digital state-machine code model (manual 12.4.18), but having analog inputs and outputs. It stems from Is binary counter Spice model available?, modified a little bit. However, I have never created a symbol for this circuit, but the colleagues at the other thread did. You may run this circuit with standard ngspice by calling ‘ngspice state-machine3b-count2.cir’. Or create a symbol and attach the subciruit model via counter.lib.

counter.lib (816 Bytes) state-3bit-count.in (537 Bytes) state-machine-3b-count2.cir (449 Bytes)

2 Likes

This was a great explanation, and I got it to work with several of the sample XSPICE models included with ngspice. It this manner it functions very similar to including regular subcircuit models.

I am now working on creating more complex devices and circuits. Your diode XSPCIE model is much easier to follow as a model than some of the original devices. Do you have any other public models you are willing to share? Such as the XSPCIE code for the state counter?

Holger converted a few community-written LTspice logic gates to XSPICE format so they can be used within ngspice. The library file can be found here:
http://ngspice.sourceforge.net/experimental/74HCng.lib

Coincidentally, that simple diode model is also based on LTspice’s own proprietary version. Although it appears simpler to understand, I highly recommend defining the epsilon and revepsilon parameters (instead of leaving them at zero) to prevent convergence issues. There’s also a neat cross-compatibility feature which lets you use LTspice’s .model card definition within ngspice (see §16.14.6 of the manual).

The XSPICE code model for the state machine (used in the state counter example) is part of the ngspice distribution, in the ‘digital’ code model section (see ngspice manual 12.4.18).

Thanks for the help.