Scripting EEschema and NGspice, general Questions

Good Day,
i have created a tool to calculate analog filter component values with active and lumped topologies. I plan to have multiple file formats than can be emitted, one of them should be a KiCAD schematic. Since i have never used the ngSpice functionallity i thought i ask here some basics and may get a kickstart.

The first question i have is on the frequency domain of ngspice:
Is it at the moment possible to do a analysis in the frequency domain? I think i remember @holger answering to somebody else that it isn’t or at least only partially at the moment. If so what would be a good method of implementing a solution that has place holders that will be much likely to be used in future versions of KiCAD. Is there a good reference on what would be needed to accomplish my goal?

Second is on the Symbol and the underlying simulation model. In LTspice i can just take a universal OPamp and be done. In KiCAD i am not aware of a unbound OPamp or a universal/ideal model that i can pick and insert in the schematic. Does something exist or is there a workaround? If there isn’t what would be the most robust way to implement this? Can i create it an commit it to the official libraries? Maybe you @Rene_Poschl can give me an estimate on how much time it would take me to do this, if at all?

What i do in LTspice looks like this:


Above is nothing special, random picked values to show the principle. The example is a second order filter with a Sallen Key topology, to build higher order filters i’ll cascade multiple entities of this behind each other using a yet to be written script connecting everything and setting the component values right in each stage. I’d be happy about general thoughts on how to implement this in a robust manner, also constructive feedback is always welcome (though in this case there might not be much to be feed back.) as well as any resources that you think might be helpfull and relevant here.
Thank You!

EDIT
This has a lot of relevant information.
This Video shows what i want to do at the beginning. It is also a very nice introduction.
Very useful information on Bsources.

1 Like

Simulation_SPICE:OPAMP

ngspice in eeschema allows doing a small signal ac simulation. The circuit is linearized by ngspice around the selected DC operating point, and then the output voltages (phase and amplitude) of all nodes are calculated versus frequency for a defined reference input node.

Plain ngspice has many more analysis types (noise, fft, etc.) but this is not (yet) supported by the KiCad/ngspice interface.

You hint at it in one of your edits. I tried to duplicate LTspice’s Universal Opamp in this library:

These filter design simulations are one of the most practical use cases for simple models. I like starting with a near ideal opamp (Level 1 with high GBWP and AVOL), and then begin lowering parameters to practical levels to make sure nothing goes wacky. Eventually I’ll plug in the manufacturer’s own model, if it’s available, to see how far off from ideal the frequency response is and tweak values as necessary.

As for scripting, I’m not 100% sure what you’re asking here. Sounds like you have a tool that can generate a filter schematic from some set of specifications and/or transfer function. And you want to be able to generate a KiCad schematic instead (or in addition to) a simple picture schematic. But you also want to be able to have it pre-set up for a simulation so that if the end-user simply opens the schematic and hits RUN it’ll just run and spit out the frequency response without having to fiddle around?

If so, all I think you need to do beyond figuring out how to generate the KiCad schematic is set up the simulation to use a simple, rather ideal model such as the Level 1 for all the Sallen-Key opamps. You can either include the full LIB file, or maybe better you can just copy/paste the .subckt for it directly in the schematic output as Text Notes so it’ll be easily bound inside your output file. You would also use Text Notes to put a default simulation command, something like .ac dec 1k 100 100meg or similar. See Section 15.3.1 of the ngspice manual for more info on that.

So today i have mostly read up a bit on how to use the integrated NGspice in KiCAD.
Thanks @Rene_Poschl for the link to the git repo! Can you give a bit more info on what this is? Will this become or is it already the official OPamp spice lib? The thing is that i want to my tool be as user friendly as possible and to work with KiCAD without the need to to install any additional software. I think that’s the most robust and reliable way to implement it.
Thanks @holger for clarifying! In the mean time i was able to read up and it seems that the ac functionality was there from the start, i think i linked above a video from 2017 when the ngspice feature was introduced.

So knowing that i startet to wonder which are my possibilities here. Since i do only need a impedance conversion it should be enough to just use Math and copy the voltage on the Node and duplicating it on the output. I know two possible solutions, either pack a subcircuit in the schematic to implement it or to use a ‘bv’ a behavioral voltage source. The latter sounded simpler to me so i started trying it out. For reference here the setup in LTspice:


You can see that the + input on the OpAmp and the output of the BV are exactly the same (as you would expect.
But when i try to implement this with NGspice the behaviour is somewhat diffrent…


sk2.sch (6.4 KB)
I actually dont know what the deal is here…most likely i fkd up :sweat:

@Ste That is awesome work! The universal OpAmp is a much needed addition! But as i wrote above i would like to have something more seamless and actually do not need that much of a functionality to simulate the filter. If it was integrated in the standard libs i would use it right away.

I more or less am just thinking out loud here. I wrote myself a program, a graphical tool written with/in wxPython that can calculate the component values of f filters lumped or active.
To provide a nice experience i alway wanted my tools to interact with my favorite programs in this case LTspice and KiCAD. so i plan to generate files with the calculated values that can be opened and used without the need of any special 3rd party tool. I actually never used ngspice before but since KiCAD has integrated i think it would be nice if you cold also run the simulation with the file my program creates.

the symbol i linked to was already in kicad since at least 5.1.0

@Rene_Poschl So you know i thought so and actually i have setup a VM to check if they are there in the default libs when i do a fresh install. I actually had to setup two of them because it turns out Virtual Box does not like Ubuntu19.10 (i chose ubuntu because i think it’s the most common in the list on the KICAD Homepage) so i went with 18.xLTS wich comes with KiCAD 4.0.7…
Tomorrow i will setup a Fedora VM…

You might just need to add the library to your setup if you had kicad installed before 5.1.0 as kicad will never touch your personal files. One of these is the library table. See Library management in KiCad version 5 section Updating the official library

If you have an old version, you might not have these PSPICE symbols installed, but they’ve been standard for a while.

I’m not a fan of B-sources, since I think they are the cause of many convergence issues in manufacturer supplied opamps. Even though for AC simulations it might not matter as much, if someone changes it to .tran you could have convergence issues. That’s why I suggested pasting in the .subckt into your schematic, like such. It’ll automatically parse this text to the SPICE netlist when the simulation runs.

Since an ideal opamp is just a G-source with a parallel resistor, you even reduce the model further if you don’t want the single-pole effect. Can even get rid of the parameters. G-source is going to be much more stable than a B-source.

.subckt ideal_opamp +IN -IN OUT
G1 0 OUT +IN -IN 1meg
R1 OUT 0 1.0
.ends

Do not use that library! it is outdated and should honestly never have gotten into version 5. (Now we can not remove it as it would break existing installations so it will stay in v5 unchanged. It even states in your screenshot that it is the legacy library)

Only the Simulation_SPICE library is setup for ngspice. (or more precisely libraries with the prefix Simulation)

On my productive system i run fedora31 with 5.1.5-1 installed but i mess quite a bit with the libraries setup so it’s not a reference. Thats why i went with the VM which should have also provided me with a “first time user experience” but yeah we now all know how that went.

I actually used bsources in LTspice quite extensively and never had a problem before. I actually found that they make thing much better/faster. But what you explain still sounds very good. It is actually what i meant above with:

I will try it tomorrow and report.

Thanks for the hint, thats good to know!

Really? It’s THAT bad huh? WHUPS!