Halite - an analog circuit simulator in ~1k lines of code

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

AUTO-ADMIN: Non-MP3, WAV, OGG, SoundCloud, YouTube, Vimeo, Twitter and Facebook links in this post have been protected automatically. Once the member reaches 5 posts the links will function as normal.
mystran wrote: Thu Sep 07, 2023 9:11 pm
timothyschoen wrote: Thu Sep 07, 2023 8:18 pm It's still very close to the Halite source, but with some components added (op-amp, transformer, gyrator, potmeter, current source). It also has a much faster solver for matrix, which makes quite a difference.
Interesting.

The original currently lives here: https://gist.github.com/signaldust/74ce ... 4a19ce8365 (https://gist.github.com/signaldust/74ce12ae67bf21a8141f9b4a19ce8365)

I can't really take your extra components back to the original, because I don't want to restrict the original with GPL, but .. I wonder how well does the OpAmp converge? Usually with sigmoids, one tends to run into problems with the trivial formulation of Newton in the clipping region especially when the signal changes polarity as the iteration diverges. If I recall correctly, a simple hack to fix this is to simply detect the divergence (ie. polarity changes and magnitude is larger) and reset the voltage to zero for the iteration in this case (which should usually then allow convergence again, though .. this might not be the fastest way overall).

The solver is really the weakest part of Halite though, one could do MUCH better, especially partitioning so that we don't need to refactor the whole thing for every iteration, but I specifically wanted to opt for simplicity. That's why it's the most simple thing that could work and very slow. On the other hand, the original stamps DO kinda assume a more intelligent solver, as some of them could be made smaller if we assume the solver is dumb... oh well.

For best performance it makes sense to partition for fast partial refactor (or rank-n update) and then JIT the whole solver into native code (the speedup over Halite's naive solver is almost always double digits), but that'd been "slightly" overkill for a simulator intended mainly as an example of how this stuff can be put together.
EDIT: looks like I'm not allowed to post links yet.. but if you find the pd-else repo, go to Code_source/Compiled/Audio/circuit~.
You can post links just fine, with new users it just adds a (temporary) popup confirmation.

The link doesn't seem to work though 'cos the tilde... but got close enough.
I would have BSD licensed it if I could have, but the solver algorithm I used (KLU) is GPL licensed so that forced me. If this is allowed under the GPL, I'll give you full permission to steal components from this, I'm also curious to see how you would improve upon the opamp.

KLU is specifically made for circuit equations, and performs very well, since it can update the factorisation instead of recalculating it every time. I believe it divides the matrix into smaller parts that are faster to solve. I've tested a few solvers, and this one was the fastest by far. Of course, a clever JIT compiled solver would beat it, I've tried writing one in the past but that was no fun.

I'm not as familiar with electronics as you clearly are, so take my implementations with a grain of salt. The components are mostly inspired by implementations from other FOSS apps or docs I could find online, but I've used the OpAmp for filtering and drive circuits, and those behaved very well. Though recently I tried to build a Tube Screamer, which did not seem to work, possibly because of what you just described.

But thanks for your work on this and for making it public, it made me dive down this rabbit hole, and it has been a lot of fun.

Post

timothyschoen wrote: Thu Sep 07, 2023 10:03 pm KLU is specifically made for circuit equations, and performs very well, since it can update the factorisation instead of recalculating it every time. I believe it divides the matrix into smaller parts that are faster to solve. I've tested a few solvers, and this one was the fastest by far. Of course, a clever JIT compiled solver would beat it, I've tried writing one in the past but that was no fun.
Yeah, dividing a matrix into smaller parts is actually not that complicated, it's mostly a matter of classifying nodes and then pivoting. This gives a huge speedup and I sort of could have implemented it into Halite too, but figured it'd significantly increase the code size which was the main optimization target 'cos... well educational simplicity.

Post

I know I'm coming to this party late, but mystran THANK YOU for this.

It's been a dream of mine to write a circuit simulator for probably almost the past 12 years, but I could never get it working. I tried once every couple of years about 5 times. Seeing Halite gave me the push to finally do it, and over the weekend, after looking over Halite (but not copying it, just being inspired by it) I finally wrote my first working circuit simulator. I'm in the process of comparing and contrasting to learn more.

I couldn't be happier. Thank you so much!

Post

If someone made a netlist reader to this for PCB CAD netlists, that would be insane :-)

Post Reply

Return to “DSP and Plugin Development”