Spice simulation via KiCAD

I have some recent experiences in running spice simulations directly from eeschema, that I thought are worthwhile to share. Before I had just played a bit with the new simulation interface, but this time I decided to use it in a real situation.

I took me quite some time, I must admit, to get the simulation to be reliable. I had a number of problems that puzzled me for a hours before I realized that the change in my workflow has a number of consequences I did not anticipate. I used to use LTspice, but that meant I had to draw the schematic twice: Once in LTspice to simulate, and once in KiCAD to do the rest.

Let me begin by stating that I like the tools/simulate feature. A lot. It is currently in the nightlies only, but I very much hope it will be included in the next stable release of KiCAD.
There is potential to save a considerable amount of time if I can use the schematic I am working on for the simulation directly and play with variations as I go along. There already was the possibility to use ngspice with .cir files generated by eeschema, but it is a bit cumbersome and less intuitive.

So what happened?
When I drew my schematic in LTspice, I implicitly left out the parts that were not of interest to the simulation, like connectors and heatsinks, for instance. I did not realize I did, but I did to save time in LTspice.
Obviously, when you are working on something that has to be built, you want connectors, heatsinks and what not, to be included in your schematic.

As a consquence of the changed workflow, I had components in my schematic that ngspice knows nothing about. At first I did not notice the errors ngspice reported, because they scrolled out of my screen too quickly. The simulation results, however, were bizarre.
Ngspice was trying to convince me I had 131kA (yes, kilo Amperes) running as the base current in a transistor, coming from nowhere. I could not find the source of so much current in the rest of my schematic, until I noticed I had a connector on that same net. Failing a spice model for the connector, ngspice apparently assumed the connector had the capability to deliver kAā€™s of current through one poor little transistor.

My fault entirely. I had not realized that I needed to disable certain parts from being included in the simulation. You can do that in the new interface, but you have to think of it. In hindsight it is obvious.

I hope sharing my experience will save you a couple of hours of wondering what the heck is going on :wink:
m

6 Likes

Thank you for the heads up. I just found the Spice interface browsing around through the forums. I had no idea it was there. So, I have downloaded ngspice and I am going to try it out.

Is there an ā€œexampleā€ schematic for doing a spice simulation in KiCAD?

Iā€™ve toyed with the idea of doing a Kicad based series as an introduction. They key audience mainly being people trying to learn this stuff through online courses. There are many paths to different goals but for a hands on person like me having an integrated solution from the beginning would have been nice. Iā€™m stuck at compiling the nightly from source. Iā€™ve wondered if picking a clean Linux distro and maintaining a live version might be the way to go. That way Iā€™d know everything will work as expected. That would also mean Iā€™d probably work on it till Version 5 is official though.

I can probably donate an example.
It must be something that is not too complicated, yet shows at least some of the doā€™s and donā€™ts.

Let me have a look, today.

I am normally running the nightlies, hermit. I have had small problems, but in general it is solved the next day or so. It happened last, oh, months ago. First Q of 2017, if I remember correctly, so I would not worry too much about stability.
If your flavour of Linux is Ubuntu, there is a repository (ppa), so you need not compile from source yourself, if you do not want to.

Steves asked for an example.
I would like to propose using this schematic as an example. It is not very useful as a device, but it does demonstrate a couple of things to note when using ngspice from EEschema. See also the attached zip below.

Comments are welcome, as usual.
CurrentsourceKiCAD-NGspice03.zip (7.0 KB)

1 Like

Well, that is my ā€˜getting startedā€™ problem. I am running Debian. It is a little behind the times. Iā€™ll probably spin up a VM with Ubuntu for this. I ran Mandrake (Mandriva ) for years because it was the Ubuntu of Redhat. When that disappeared I went for ā€˜stableā€™.

Yep, I like debian as well, but there is much more to do yourself than there is with ubuntu. A 'buntu VM sounds a reasonable compromise. I personally prefer the Gnomebuntu variant, because running Gnome in both environments is easier for compatibility.
Good luck.

I was wrong in my remark that SUBCKT is currently not supported by the gui.
It is.
I have changed my demo project accordingly and fixed the download above. If you have installed the previous version, please add the new version (with the number 02 in the filename) as well.

Thank you for posting this. I have downloaded and will try it out.

1 Like

Thanks for the example file! I was having issues running simulations components not included in the spice library and your model helped me realize the requirement for the .lib model for each part.

Where did you get these files or learn to write them. I learned how to use LTspice just last night to test my circuit but would like to contain this step in KiCad.

Could you otherwise point me towards a resource that would help me find or generate or download these models. I was looking around for a KiCad or ngspice mode library (something that contains the .lib files referenced in the ā€œedit spice model > model > select fileā€¦ā€ dialogue. This is something i feel would have been answered in a tutorial but I canā€™t seem find any that go beyond using standard components (R, C, L).

Here is a tutorial: http://ngspice.sourceforge.net/ngspice-eeschema.html

I think the great drama here is to have the ā€œMODELSā€ā€¦

That is the tricky partā€¦

And once you have them collectedā€¦ where do you put them all
so as the system can trace the pile of stuff you collectā€¦

The general topic of ā€œSpice Modelsā€ is about an order of magnitude more complicated than the topic of ā€œFootprintsā€. There is some discussion about the complexity in old threads on this Forum. Just for starters, the various companies who have implemented Spice-based simulators often have their own extensions and improvements to the Spice syntax. In most cases it isnā€™t difficult to convert a model designed for one version of Spice to run on another version.

Some manufacturers supply Spice models for their components. A few individuals have developed libraries of Spice models and make them available online. Also join the Yahoo Userā€™s Group for LTSpice at LTSpice Group . There are many models in their ā€œFilesā€ area, and examples in the discussion threads.

Dale

As dchisholm and PKTKS have pointed out, the difficulty is in the models. They are absolutely correct.

While it is true that manufacturers supply models for some of their parts, theyā€™re often made for some specific spice implementation, such as Pspice, for example. Pspice does have a number of built-inā€™s that are not in ngspice, which explains why some of the models that were written for Pspice do not work in ngspice and are not easily portable either.

It is all a bloody mess and a great nuisance, but, I fear, the thinking behind all this is closed source minded. LTspice is offered for free, but it is closed source and it assumes you are doing all work in LTspice itself. Openness is not a priority.

It would be very efficient if all the effort in adding built-ins, making models etc. would all be directed towards one single open source version of spice, but at the moment that has proven to be a step too far for the closed source mindedness.

We will have to live with that for the time being. I hope that, one day, we will see that the Pspice and LTspice people offer their efforts to the open source community. In the mean time, I will keep on dreaming :wink:

That said, it would be neat if the kicad community would be able to share a collection of spice models to go with ngspice, but it will hardly be complete, I fear. If such a collection exists already, I would love to get a pointer to it, because I have missed it.

Also, spice models are usually intended to be used for a specific purpose and sometimes do not accurately model the real life behaviour of the part. Many models model the behaviour of a part that is behaving as intended under normal circumstances, but do not accurately model the part when it is used beyond its limits, for instance. Things like failure modes, noise modelling, lockups, power supply rejection, etc. are not always modelled accurately in the models that are supplied for free.

Hence it is prudent to acquire the skills to write models yourself, which would be perfect in combination with some sort of shared collection within our community. At the very least, in my view, one should be able to judge the quality of supplied models.

m

As an example of how complicated accurate modelling can be, this article will serve as an illustration.
I have been looking into accurately modelling loudspeakers myself, but it is no mean feat.
See: Article by JĆ¼rgen Micka about spice modelling loudspeakers.
m

This is not correct any more!

ngspice-28 well reads PSPICE model files and does the translation from PSPICE to ngspice syntax automatically. All of the manufacturersā€™ PSPICE models are now available for ngspice, in addition to TINA ones. If something is missing or does not work, just give a hint here or at the ngspice user forum.

The only issues we cannot solve are encrypted models. Encryption and Open Source are diametrically opposed , so I am not aware of any solution.

In addition ngspice-28 now contains the VDMOS model that has been famous in LTSPICE to model MOS power transistors.

The KiCad developers however decided to stick to ngspice-26 for the upcoming release. Therefore I will give a short explanation on how to switch from ngspice-26 to 28 in my tutorial.

This is wonderful news.

EDIT: Omitted needles blahblah

I will certainly give Pspice models another go, because if Pspice models work, that would solve a lot of trouble for me. Thanks holger.

m

That would be most welcome. Appreciated!

EDIT: Couldnā€™t wait. Found this thread:

m

Testing libngspice 28. It seems to be working from eesched.
Is there any way I can easily verify which version of libngspice is being loaded by eesched?
Do you have a quick setup that would work in 28 but not in 26?

m