Issues with KiCad Simulation of Ferrite Core Inductors and Transformers

Hello everyone,

I’m relatively new to simulating circuits in KiCad and ngspice, and I’m currently facing a challenge with modeling ferrite core inductors and transformers. Specifically, I’ve been attempting to use the ngspice Magnetic Core model and Inductive Coupling model to simulate saturation behavior. Here’s what I’ve done so far:

Ferrite Core Inductor:

I’ve created a model for a toroidal ferrite core inductor. However, it only seems to work properly when a current source is applied. When I attempt to simulate it with a voltage source, the simulation aborts due to an ‘unrecorded case’ error. Surprisingly, the same circuit simulation works perfectly in a separate ngspice simulation, regardless of whether a current or voltage source is used.

Here is the code snippet for the KiCad library:

.SUBCKT Magnetic_core 1 2 3 4
.model primary lcouple num_turns=5.0
a1 (1 2) (3 4) primary
.ENDS Magnetic_core
.SUBCKT Magnetic_field 1 2
.model iron_core core (H_array = [-1000 -500 -375 -250 -188 -125 -63 0
+ 63 125 188 250 375 500 1000]
+ B_array = [-3.13e-3 -2.63e-3 -2.33e-3 -1.93e-3
+ -1.5e-3 -6.25e-4 -2.5e-4 0 2.5e-4
+ 6.25e-4 1.5e-3 1.93e-3 2.33e-3
+ 2.63e-3 3.13e-3]
+ area = 3.6e-05
+ length = 0.0644)

a2 (1 2) iron_core
.ENDS Magnetic_field

And here is the code for the separate ngspice simulation:

.save all
.save @A2(i)
.probe alli
.tran 100n 100u
.option method=gear
.option maxord=4
.model windings lcouple num_turns=5.0
.model iron_core core (H_array = [-1000 -500 -375 -250 -188 -125 -63 0*
+ 63 125 188 250 375 500 1000]
+ B_array = [-3.13e-3 -2.63e-3 -2.33e-3 -1.93e-3
+ -1.5e-3 -6.25e-4 -2.5e-4 0 2.5e-4
+ 6.25e-4 1.5e-3 1.93e-3 2.33e-3
+ 2.63e-3 3.13e-3]
+ area = 3.6e-05
+ length = 0.0644)

I1 VIN 0 SIN(-0.5 1 10000)
R1 VIN MED 1
R2 MOT MKD 1
R3 MMF MAT 1
A1 MED 0 MMF 0 windings
*A2 MAT MOT iron_core *

.control
run
plot I(R2)
.endc
.end

Transformer:

Additionally, when I create a transformer model in KiCad, it consistently interrupts the simulation with the same error unless I connect a very high ohmic load (1GOhm) to the secondary side.

Has anyone else encountered a similar problem or can offer insights into what might be causing these issues in the KiCad simulation tool for both cases? I would greatly appreciate any guidance or suggestions you can provide.

If it is working in discrete ngspice, you may put all of the inductor circuitry into a subcircuit and attach this to the inductor symbol.

Simulating with a voltage source will only work if you have a series resistance in your inductor. Otherwise you cannot determine an operating point.

If you have a transformer, both the primary and the secondary side will need a dc path to ground. Otherwise ngspice (and all other spices) cannot determine which is the eletric potential of this floating part.

0 Ohm resistors are not allowed in spice, in ngspice they will be converted to 1 mOhm resistors.

There is an transformer example in ngspice / ngspice / [2275fb] /examples/various/transformers1.cir. If you replace the Rreluctance by the core model, you might achieve saturation behavior.

I have now tried to combine both models of magnetic core and inductive coupling with all the additional circuitry into one subcircuit for KiCad. This is what it looks like now:

Here is the code for the Toroidal_inductor library:

SUBCKT Torodial_inductor SUP RES
.model windings lcouple num_turns=5.0
.model iron_core core (H_array = [-1000 -500 -375 -250 -188 -125 -63 0
+ 63 125 188 250 375 500 1000]
+ B_array = [-3.13e-3 -2.63e-3 -2.33e-3 -1.93e-3
+ -1.5e-3 -6.25e-4 -2.5e-4 0 2.5e-4
+ 6.25e-4 1.5e-3 1.93e-3 2.33e-3
+ 2.63e-3 3.13e-3]
+ area = 3.6e-05
+ length = 0.0644)

R6 SUP MED 1
R7 MOT 0 1
R8 MMF MAT 1
A1 MED RES MMF 0 windings
A2 MAT MOT iron_core

.ENDS Torodial_inductor

After adding the voltage source and the resistor in series with the inductor, the circuit still gives me the same error as before when used with the voltage source. With the current source it works as before with the same result, no errors.

Do you have any other insights as to why this might be occurring, or any alternative solutions?

Please zip your project and post it here.

I would love to, but the platform doesn’t allow new users to upload files, so I can’t upload it here. But I have created a wetransfer link, you can download the zip file with the project from there.

Here is the link: WeTransfer - Send Large Files & Share Photos Online - Up to 2GB Free

I have loaded your Eeschema file into KiCad7.99, run the transient simulation and immediately get the following output:


So what is the problem right now?

This is very interesting because in my case I get an error when simulating the same circuit. I have version 7.0.7.

Could the problem be due to the fact that I am using a different version of KiCad or different operating system?

Either by platform usage or stray mod, you are now a basic user. You can edit the previous post with the file if you wish. Might be good for posterity as that link will probably disappear at some point.

Welcome to the forum.

Hi,

does your transformer work in the latest kicad v8?

I get:

.model xm1:ferrocore core(h_array=[    4.40000000000000036e+00  ]  b_arr ...
Unknown model type core - ignored
Error on line 0 :
a.xm1.a1 net-_l1-pad3_ 0 xm1:ferrocore
MIF-ERROR - unable to find definition of model xm1:ferrocore
Warning: Model issue on line 0 :
.model xl1:coiloncore lcouple (num_turns    1.00000000000000000e+00  ) ...
Unknown model type lcouple - ignored

while running the same netlist from the command line works.

Seems to be an installation problem.

The error message is typical for not finding the code models.

Depending on the OS, ngspice and KiCad/ngspice do not share the same library paths.

It is the default deb v8 release channel.

http://ppa.launchpad.net/kicad/kicad-8.0-releases/ubuntu/dists/focal/InRelease

If I look at Index of /kicad/kicad-8.0-releases/ubuntu/dists/focal/main/binary-amd64, there is no mention of analog.cm etc.

I have contacted the package maintainer of KiCad in Debian and asked for support.

Found the issue, with upgrade to kicad8 the dependency of kicad v8 does not trigger the installation of the libngspice-kicad, in my case I had to install it manually and now it works:

$ sudo apt install libngspice-kicad
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  libngspice0
The following NEW packages will be installed:
  libngspice-kicad

@holger I would like to ask you if you can review ngspice merge request #15 now as kicad with ngspice is working better and better and so would really like kicad to replace the command line driven ngspice :wink:

Since the model provides smooth and monotonic function convergence is stable even in deep saturation, and time step can be bigger. I’ve added an example how to easily obtain ferromagnetic, or iron, …, material parameters:

As you are addressing me directly:

The merge request touches an old discussion we have had several years ago.

We might continue the discussion in a private session.