dsharlet / LiveSPICE

Real time SPICE simulation for audio signals
MIT License
419 stars 61 forks source link

Fix switch initial conditions #162

Closed dsharlet closed 1 year ago

dsharlet commented 1 year ago

@Federerer I am curious to see what you think of this solution to #148. It adds a fake symbolic resistor for open switch circuits that has a large but finite value when solving for initial conditions, and is actually infinity when running the simulation.

This seems to fix all cases of initial conditions failing due to switches (two example circuits, plus your reproducer from #148).

I think this solution is quite a hack, and exposes some really weird behavior in the solver that needs a fix that seems like nonsense.

I am particularly curious if this eliminates the need for your random reordering of components to improve stability. It's possible that the extra stability is only needed because the simulation is starting so far away from the normal operating point...

Federerer commented 1 year ago

I am particularly curious if this eliminates the need for your random reordering of components to improve stability. It's possible that the extra stability is only needed because the simulation is starting so far away from the normal operating point...

So, during the work related to the dynamic parameters I noticed, that most of the circuits that I've tested seems to start fine from everything at 0 as an initial state (I've accidentally broke the initial conditions solver 😁), so this seems unrelated, however I'll compare the results after fixing the steady state solver. This fix, however looks fine, maybe it's a hack, but it's better to have a working solution than a perfect-looking solver that doesn't work 😉

dsharlet commented 1 year ago

Most circuits are OK starting from 0 (although they tend to make a very large "pop"/discontinuity... just like an old stereo, very realistic :) ). But some will fail to run at all without initial conditions, so it's important.

I'm going to merge this, because I want to make and test a new release, and I think this should be in it.