Alf's Musings #36: Of Spice And Magic

Alf's Musings #36: Of Spice And Magic

A bit out of context

I firmly believe that engineering advances are born from imagination, and that nothing feeds the imagination more than reading and discussing things to come. An engineer reading about something that doesn’t exist (but it could) in a science-fiction book is the nerd equivalent of telling a teenage influencer wannabe not to eat detergent.

China discovered this years ago and it started promoting Science-Fiction among their youth, which produced one of the best books in the last decade (in my humble opinion): The Three-Body Problem. And coincidentally, it produced an increase in technology. Correlation? Nah, probably not. Similarly, the promotion of football and the lack of industry/technology (or crappy salaries) in Spain are probably not correlated either.

On this topic I wanted to comment on one of the adagios that came out of Science-Fiction, Clarke’s third law: “Any sufficiently advanced technology is indistinguishable from magic.”, and I wanted to link it to other term with a Sci Fi name, though not in origin: SPICE.

Many power electronics engineers often work with a variant of SPICE: Ltspice, Psim, Pspice, Simetrix, etc. But few are familiar with the inner workings of the models.

Also, many power electronics engineers often work with magnetics in their designs: transformers, flyback, inductors, common mode chokes, etc. But few are familiar with the inner working of the electromagnetic field, and its effect on these components.

The combination of these two phenomena is a breeding ground for some businesses that try to automate the interaction between SPICE and magnetics, but covering it with a veil of “black magic” instead of trying to bring light to people’s knowledge gaps.

Because of that I decided to start automatic SPICE models in OpenMagnetics , starting with SIMBA, Power Electronics Simulation Software , LTSpice and Ngspice, but as I don’t want to become what I criticise, I decided to share all the knowledge I learn from it.

Which takes us to this article, where I want to discuss? how to model magnetics in SPICE using ladder networks.?

Easy spicey

We need start by mentioning that a network of ladder is not the only way of modeling a magnetic component and its non-idealities in a circuit simulators, nor it is the best one: Other implementations can be done using special components that allow generating frequency dependent impedances, like the voltage dependent voltage source in Ltspice, the magnetic circuit capabilities in Simba or PSIM, or using advanced behavioral models in verilogA.

A ladder network is, however, the only method which can be used in all simulators, as is made of simple RL components. A verilogA model can work in Ngspice, but not in Ltspice, a voltage dependent voltage source can be used in Ltspice but not in Simba, and a magnetic circuit can be used in Simba, but not in Ltspice.

A ladder network is a subcircuit that tries to model the non-linearities of the magnetic over frequency by connecting in some special way a number of resistors and inductors that will model the losses in the real component.

A typical example is how these networks can model the AC resistance of the windings of the component, which increases with the frequency, and whose slope is not constant as different effects enter the game: skin effect, external proximity effect, or internal proximity effect (e.g.: in the case of Litz).

The way this ladder achieves this effect is by playing with the impedance change of the inductor over the frequency, which allows changing the distribution of currents in a parallel circuit as the frequency varies, and this current then flows through the associated resistor, which modifies the overall resistance.

Adjusting these values might look like black magic, and it's even presented as almost that by companies who profit from selling modeling software, and even maybe at some point of the past it was, but in this day and year, with the open-source tools available to everyone of the internet, adjusting the parameters of a RL network to mimic some curve is quite easy. Let's see how

The first step is of course having a frequency curve to fit into. Here we have two approaches: modeling and measuring.

Measuring approach is the most common one, and it can easily be done by measuring the desired effect with an impedance analyzer and exporting the data. Some commercial analyzers even include some circuit fitting in their software, like Omicron's Bode 100, allowing users to fit their measurements over some circuits (although not ladder networks yet, up to my knowledge, Tobias Schuster we need to talk!). The advantage of these models is that you are actually modeling the reality (ok, the measurement of the reality), which is almost always better than modeling. The disadvantage is of course, you need to assemble the prototype, so its utility is limited in the design phase by how fast you can assemble a sample and your stock.


Circuit fitting in Bode 100/500

Modeling approach implies being able to model the desired effect, like the AC resistance of a coil, or the core losses of the core. It has the advantage that we don't need to actually have the parts to assemble a sample, as we are only limited by our imagination and our modeling ability. In this regard, the more accurate a model is, the better our final ladder network will be. It won't be the same using Dowell or Steinmetz to calculate the curve to fit, than 2D Albach's or iGSE, whose accuracy and applicability is larger.

In the measuring case it is quite straightforward, as the hardware (the aforementioned Bode100 for example) will give us the values over frequency that we need to fit. In the case of modeling, my recommendation is running a simple script (in Python, what else?) that will create a frequency array to sweep, and will evaluate the model at each frequency. In both cases we will end up with a list of points that represent the frequency response that we want to model in SPICE

And now comes the black magic. We need to find the values of inductance and resistance that will mimic that curve. This is done by a process called curve-fitting.

Curve fitting is a mathematical process that aims at finding the unknown coefficients of a given equation that minimize the distance between the result of that equation and a given list of values. It is a way of equation solving where the number of unknowns is smaller than the number of known results.

This means that we need three things:

  1. The known values we want to fit in, which we obtained by either modeling or measuring.
  2. The equation we want the known values to fit in, which would be the symbolically solved electrical circuit of our ladder network.
  3. The algorithm that will fit one into another.

For the first requirement, we have two methods of obtaining these values, as we have discussed.

For the third one, we can use the “Levenberg-Marquardt” algorithm, also known as the damped least-squares, which can work with non-linear cases, like the ones we are describing in this article.

Don’t be afraid of math, dear reader, for in this case it is covered in nice methods: I can recommend scipy.optimize.curve_fit library in Python, or levmar library in C++. If you are looking for a library for Matlab, I would recommend you to download Python, spend a couple of hours getting familiar with it, and never open Matlab again.

Finally, for the second requirement we will use ladders of inductances and resistances as show in the image:

This will produce a equation for the curve ft such as:

DcResistance + parallel(L1, R1 + parallel(L2, R2 + parallel(L3, R3 + parallel(L4, R4 + parallel(L5, R5)))))

Regarding the number of elements in the ladder I recommend iterating over different numbers of elements, starting in a large number (e.g.: 10 steps) and removing them one by one, while checking the error obtained in the fitting. Once removing a step of the ladder increases the error substantially, we will use the number of steps of the previous iteration.

The presented example was implemented for the winding AC resistance. For core losses we can also use this ladder, or a different one as the one used by Dr. Ridley in his software.

Image courtesy of Dr.Ridley

This fitting algorithm can be quite sensitive to the initial value, so it is also recommended to do a sweep of initial values and check the error for each one, keeping the ones that produce the best fit.

Once we have the three requirements, we can run them together (as always, take a look at OpenMagnetics’ code if you are not sure how) and we should obtain the inductances and resistances that we need to enter in our ladder.

The last step can be the most tedious one, which is actually creating the SPICE code, which can be done automatically or manually. My recommendation would be to automate the process with some fixed name parameters, and then add the values for each case in these parameters.

I know this is not the easiest stuff in the world, but it can be done with a bit of Python skill. Or you can just go to OpenMagnetics, design your magnetic there in the beautiful GUI, and just download the SPICE models produced by the method I just explained to you.



Graham Gunderson

Free Electricity. Seriously!

2 天前

Is the image some AI generated crap? It's awful to scroll thru so many posts doing that to people. It's worth unfollowing, at least to me. Ugh.

marcel hendrix

Scientist (retired)

4 天前

> a magnetic circuit can be used in Simba, > but not in Ltspice Can you elaborate a little bit? (What does Simba offer for magnetic modelling that can't be reproduced in LTspice/SPICE?)

回复
Jon Izkue Rodriguez

Hardware Engineer & Capacitor Educator / Product Management @ Würth Elektronik eiSos GmbH & Co .KG

5 天前

Nice article, horrible nightmarish image. I want to get started with curve fitting for my caps, what Visual Basic library do you recommend for excel? or a plugin? ??

要查看或添加评论,请登录

Alfonso Martínez de la Torre的更多文章

  • <Uncomfortable opinion warning> Obsolescence

    <Uncomfortable opinion warning> Obsolescence

    Some decades ago there were people whose job was to use computers and enter the process requests needed by others that…

    7 条评论
  • Alf's Musings #35: Of Capacitance Modeling and Leyden Jars

    Alf's Musings #35: Of Capacitance Modeling and Leyden Jars

    A bit out of context Did you know that the first capacitors date from 1745? They were called Leyden Jars, and consisted…

    15 条评论
  • Alf's Musings #34: Of Capacitance I

    Alf's Musings #34: Of Capacitance I

    A bit of context As I recently finished the capacitance model of OpenMagnetics, I wanted to write an article about what…

    5 条评论
  • Alf's Musings #33: Of Models and Timelines

    Alf's Musings #33: Of Models and Timelines

    A bit of context Long time no write! I apologize for the large hiatus in the publication of Alf’s Musing’s, I have been…

    11 条评论
  • Syllabus for a Modern Approach to Magnetics.

    Syllabus for a Modern Approach to Magnetics.

    Block I: The Physic of Magnetics What is a magnetic component? What types are there? How we should look at magnetics…

    22 条评论
  • Alf's Musing #32: Magnetizing, Stray, and Leakage Inductance

    Alf's Musing #32: Magnetizing, Stray, and Leakage Inductance

    Lesson 12 - Intrinsec Effects in Magnetics Let’s start by taking a look at the different effects that happen in a…

    24 条评论
  • Alf's Musings #31

    Alf's Musings #31

    A bit of context I must admit that this Alf's Musing has been inspired by the Spanish Engineering job market during my…

    13 条评论
  • Alf's Musings #8

    Alf's Musings #8

    A bit of Context This article was published originally around one year ago, when I decided to make public the little…

    10 条评论
  • Alf's Musing #30: Making BAM III, the Core of the Business

    Alf's Musing #30: Making BAM III, the Core of the Business

    A bit out of context Today I want to talk a bit about sins. Since I was a kid I loved stories and mythology, and it…

    8 条评论
  • Alf's Musings #29: The Art of Open-Source Product War

    Alf's Musings #29: The Art of Open-Source Product War

    A bit out of context If by any chance, dear reader, you are a metalhead, I recommend listening to Sabaton’s The Art of…

    3 条评论