Using NGSpice - missing primitive libraries symbols

I am trying to simulate a low-passing filter in Eschema 5.99 + NGSpice 31, the schematic is attached. I have some question:

  1. I didn’t found a current source controlled by current on Spice library of Eeschema. Is it missing?
  2. The Spice is warning me that it didn’t find the capacitor or op. amp. circuit. Did I used the wrong library?

circuit.kicad_sch (30.4 KB)

Application: KiCad Schematic Editor

Version: 5.99.0-unknown-b84d1456d5~109~ubuntu20.04.1, release build

Libraries:
	wxWidgets 3.0.4
	libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3

Platform: Linux 5.11.0-34-generic x86_64, 64 bit, Little endian, wxGTK, ubuntu, x11

Build Info:
	Date: Sep 15 2021 00:33:16
	wxWidgets: 3.0.4 (wchar_t,wx containers,compatible with 2.8) GTK+ 3.24
	Boost: 1.71.0
	OCC: 7.3.0
	Curl: 7.68.0
	ngspice: 31
	Compiler: GCC 9.3.0 with C++ ABI 1013

Build settings:
	KICAD_USE_OCC=ON
	KICAD_SPICE=ON

On windows, it is still where it should appear, but I am not sure about the linux version.

edit:

There is also a new library called Simulation_SPICE, which may be worth a look.

But this is the current source. I want the current source controlled by (dependent of) current.

In my schematic is placed the current source controlled by voltage.

There’s an FSOURCE (current-controlled current source) in the PSPICE library but not in the Simulation_Spice library.

The PSPICE library is more or less deprecated, and the symbols are kind of ugly, but it has some things that aren’t in the Simulation_Spice library. The Simulation_Spice library is newer and nicer, but missing some symbols, namely the dependent sources.

I think you might have to mess with the model parameters when you use a symbol from the pspice library. Namely you’ll have to set the spice_primitive to F and the spice_model to whatever gain you want, e.g. 1 for 1 A / A

Thanks @gkeeth, l think just rename the “current dependent of voltage” by F** will do…

I will have the current status of the complete KiCad Spice library. I am also having the issue with the capacitors and missing others spice primitives (Laplace, sum, and so on).

For the capacitor, you need to change the value from 1n1 to 1.1n. ngspice doesn’t understand RKM notation.

For the opamp, you need to provide a model for it, there is no built-in spice model. You can models for specific opamps from manufacturers. For playing around, I tend to use @ste’s handy universal opamp model from here: Universal Opamp SPICE Macromodels

  1. Download that file and put it somewhere safe (I have a spice model library folder).
  2. Go to U1 -> Properties -> SPICE Model… and go to the model tab
  3. select the library you downloaded
  4. In the model dropdown, you can choose between lvl1 and lvl2. Level 1 does not have power rails, it just has the two inputs and the outputs. Your symbol has power rails so I would choose lvl2, which models clipping to the rails.
  5. Check alternate node sequence: the symbol has pins 1: noninverting input, 2: inverting input, 3: Vout, 4: VCC, 5: VEE. For the spice model, pins 1 and 2 are the same, but 3-5 do not match the symbol. So we use the alternate node sequence “1 2 4 5 3” to remap them.

The next errors you will run into are:

  1. ngspice doesn’t understand the gsource. The quick way to fix this is to change the value from GSOURCE to your desired transconductance gain, for example 1 to get 1A/V. If you do this, it will work because ngspice/kicad guesses that a symbol called G1 is a gsource and uses its value field as the spice parameter. Another way to fix this which is a little more explicit, would be to add 3 fields to the symbol: Spice_Primitive:G, Spice_Model:1, and Spice_Netlist_Enabled:Y. This does the same thing but kicad/ngspice doesn’t have to make any guesses.
  2. ngspice doesn’t understand the vsource V1. same thing, either change the value from VSOURCE to your dc value, or add the explicit fields.
  3. ngspice doesn’t know what a .trans simulation is. You probably meant to write .tran.
  4. Ngspice thinks your test point TP1 is a transmission line and is upset it doesn’t have a model. You can fix this by deleting the test point (unlike some other software, you don’t use a testpoint to probe a symbol in kicad/ngspice, you can just plot the signal).

Then it should work :slight_smile:

Lista 04 - exercício 01 - condicionamento de sinais.kicad_sch (29.4 KB)

1 Like

ngspice-35 does understand RKM notation (ngspice-31 is used here), if ‘Simulation Settings’->‘Compatibility mode’ is set to PSPICE and LTSPICE.

1 Like

Cool! Didn’t know that.

Thanks guys…

I decide to move on and help adding some new symbols to the library.

I manage to run voltage/current sources controlled by voltage but I am failing on the controlled by current ones. Is H* and F* sources not available on NGSpice v31?

Circuit: KiCad schematic
Error on line 9 :
h1 net-_h1-pad1_ 0 net-_f1-pad4_ net-_h1-pad4_ 1
unknown parameter (-_f1) 
Error on line 10 :
f1 net-_f1-pad1_ 0 net-_e1-pad3_ net-_f1-pad4_ 1
unknown parameter (-_e1) 
Background thread stopped with timeout = 0
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
Fatal error: f1: unknown controlling source net
doAnalyses: no such parameter on this device
run simulation(s) aborted

I am attaching my proposed symbols (I planned to submit to the official library). Could someone have a look?
test.kicad_sch (38.9 KB)

I am just using 31 because of native package on Ubuntu 20.04. Is there some PPA for most recent NGSpice? I want want to compile by my self or create libraries compatible with version that not expert users could install.

I don’t think that’s the right format for an FSOURCE. I’m looking at the ngspice manual here which indicates that you specify a gain and the name of the controlling voltage source, so you should add an external voltage source of 0 volts that acts as an ammeter. Like this, where V2 is the controlling voltage source:

image

However I get this error when I try it, so I must be doing something wrong. Perhaps @holger can help.

Circuit: KiCad schematic
Error on line 0 :
a$poly$h1 %vnam [ net-_h1-pad3_ ] %vd ( net-_h1-pad1_ 0 ) a$poly$h1
MIF-ERROR - model: a$poly$h1 - Bad real value
Background thread stopped with timeout = 0
Error: circuit not parsed.

Thanks, I understood: the symbol F*/H* will the measurement part to the controlled source. I just have to dig and understand a better way to put this on the KiCad fields formats (the two parameters needed).

Someone knows if is possible to fix SPICE text line model for a symbol. For example: H1 Net-Pin1 Net-Pin2 SOUCE GAIN but want want the SOURCE and GAIN be fields of my symbol instead of only Value.

A possible solution is to use subcircuit models.

The attached files use such models, H and F sources embedded into a subcircuit each, including the measurement zero voltage sources.

HFsources.zip (5.9 KB)

Ah, the separate voltage source and F-source work like this:

The issue was that there was no current path through the H source, because ngspice has no way to know the ammeter pins are shorted, unless you write a subcircuit model like holger describes.

I don’t know much about spice in KiCad, but I would prefer smaller symbols, about the same height as a normal resistor in KiCad. Especially the rotated square looks extremely big compared with the rest of KiCads symbols.
The old and big symbols have to be maintained for the exising userbase even though they do not fit well in the current KLC

Work on libraries is also suspended for quite a long time already:

Yes, it would be better to base the symbols on the Simulation_Spice library which has more attractive and reasonably sized symbols:

About the size, sure. I was testing the symbol format but I had based in big one. I am in favor of standard.

I just don’t know yet the better way of have a “gain” field in the symbol and have it passed to the Spice code line.

I would set up the symbol like this:
image

That way it’s set up as a G-source with a gain of 1, and the gain is easily editable.

You can get the Spice* fields added automatically by clicking Spice Model… and then setting it to a voltage source or really any other kind of source. Then you can just edit the values.

For the H- and F-sources which require you to pick a controlling voltage source… that’s harder. There is no default value that will definitely work. I would probably choose V1 as a good default, so the Spice_Model field would be V1 1.

It doesn’t seem to understand µ though (unlike LTspice; see patch of KiCad 5 fork for that).