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

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

Do you have a struct for the input signal to share? I'm used to place it in the U vector, but I'm a bit clueless how to place it in your code.

Thanks!

Post

Audiority wrote:Do you have a struct for the input signal to share? I'm used to place it in the U vector, but I'm a bit clueless how to place it in your code.
If you're talking about Halite, just look at what the function generator does: it puts the signal into a variable that it stamps as a "dynamic load" to the mna.b vector, so it gets replaced with the actual value every time it solves the system.

While the function generator uses a callback function to generate the input signal, you could just modify that part of the code to stream from a buffer or something instead.

Post

Thanks mystran. I totally missed out the function generator!

Post

I was wondering if an optimized version could be better done in Python.
Explaining myself. Let's say the JIT is LLVM. It may actually be easier to target clang AST instead of LLVM IR. From the AST, the matrix can be statically defined (and using Eigen, for instance), the derivatives could also be done symbolically and perhaps even some matrix inverses directly as well.

Post

Miles1981 wrote:I was wondering if an optimized version could be better done in Python.
Explaining myself. Let's say the JIT is LLVM. It may actually be easier to target clang AST instead of LLVM IR. From the AST, the matrix can be statically defined (and using Eigen, for instance), the derivatives could also be done symbolically and perhaps even some matrix inverses directly as well.
I honestly don't really understand any of what you are trying to say here... I mean I obviously understand what the words mean, but the underlying logic completely evades me.

Post

I'm just thinking at what would be the easiest to target. LLVM IR or Clang AST, and the fact that in Python there are potentially more tools for automatic differentiation that in C++ (although there is DCO). So it may be easier to generate optimized code in Python than in C++.

Post

Miles1981 wrote:I'm just thinking at what would be the easiest to target. LLVM IR or Clang AST, and the fact that in Python there are potentially more tools for automatic differentiation that in C++ (although there is DCO). So it may be easier to generate optimized code in Python than in C++.
Well, you only need automatic differentiation if you want to allow components with arbitrary equations that the user can type in (and automatically generate Newton evaluation for these). This runs into a whole new can of worms though. For example, if you try to implement something as simple as a diode in Spice (eg. I've tried in LtSpice) using a voltage controlled behavioural current source, you'll find that you run into all kinds of convergence issues that the built in diode doesn't have.

As well as LLVM IR goes, it's just SSA and if you can't generate SSA you probably can't generate an AST either... so personally I don't see a point why you'd want to draw clang into the mix (although I have to say I'm not convinced I want to use LLVM either; let's not discuss that though).

Post

The link seems to be dead. Is this still available somewhere?

Post

I don't think humanity deserves my code anymore.

Post

mystran wrote: Sat Oct 27, 2018 7:32 am I don't think humanity deserves my code anymore.
Is it worse than marshes and bogs of yesterdays Finland, above which todays Finland resides? (or so J.V.Snellman would say?)
~stratum~

Post

I had a git copy, but if mystran wants it gone, I will also remove (let me know!).

Just an FYI, ATK-modelling-lite is a similar project (but not as optimized as mystran's Halite).

Post

Miles1981 wrote: Tue Oct 30, 2018 11:45 am I had a git copy, but if mystran wants it gone, I will also remove (let me know!).
I frankly don't give a shit either way.

Post

Haha, fair enough ;)

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.
If anyone is still interested in this, I've created a pure-data external based on Halite, so you can simulate analog circuitry in pure-data. You can also control voltages, currents, resistors and potmeters with audio signals.

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.

This will be included in the ELSE library of externals, but if you're interested you can find the source code here: https://github.com/porres/pd-else/tree/ ... io/circuit (https://github.com/porres/pd-else/tree/master/Code_source/Compiled/audio/circuit)~

Here is a fuzzface in Pd:
Screenshot_2023-09-06_at_04.27.21.png
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 do not have the required permissions to view the files attached to this post.

Post

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

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.

Post Reply

Return to “DSP and Plugin Development”