The purpose of LiveSPICE is to simulate circuits for audio signals in real time. Simulating a circuit requires solving for the relationship between the current state of the circuit and a new input signal, to produce the new state of the circuit. This post describes the algorithm LiveSPICE uses to evaluate this relationship quickly enough to run in real time.
My hope is that this post fills what I found to be a gap in the literature I could find on solving this kind of system. It took me several months to converge on this solution (numerical analysis pun!); maybe this approach is supposed to be obvious, but it certainly was not obvious to me.
To be more specific, the precise problem this post solves is to solve the system of equations produced by analyzing a circuit via modified nodal analysis (MNA) or other similar techniques. The basic result of a circuit analysis technique is a differential algebraic equation (DAE) describing the behavior of a circuit of the following form:
Note that this representation has split the algebraic part of the system into linear and explicitly non-linear parts. This allows the system to be statically analyzed further than is possible with a single term representing both linear and non-linear algebraic equations.
Also note that this representation requires that is constant w.r.t. , i.e. the differential terms of the system are linear. This is the case when working with the standard circuit components.
The other part of the problem we wish to solve is to be able to find these solutions very quickly. Our overall strategy to make this fast is to break the problem into two pieces of work: one piece of work can be done once at analysis-time; the other piece of work must be done at run-time for each sample we process. Our goal is to minimize the amount of work to do at run-time, which might mean moving expensive work to be done at analysis-time.
Discretizing the system
The goal of a numerical simulation of the system is to determine values for satisfying (1) at some regular sampling of . The first step in solving this problem is to eliminate the differential equations. To do this, we apply a numerical integration technique, such as the trapezoidal method used in LiveSPICE:
This introduces a relationship between the state of the circuit at the previous timestep and the current timestep . To use this directly, we have to solve (1) for . It is tempting to simply solve (1):
However, this solution is not valid, because is not invertible; only some of the equations in (1) are differential. Even worse, the non-zero rows of (the rows corresponding to differential equations) may not be independent (there may be more differential equations than unknown differentials). To work around this problem, consider this representation of (1):
Row reduction of the system above results in a new system of the form:
Note that in the above, the number of equations remains the same, we've just split them into two groups. The first group gives us an ordinary differential equation:
where is not singular, meaning we can solve for similarly to (2), and apply the trapezoidal rule (or another numerical integration scheme). We also get an algebraic equation from the second group:
Combining the above algebraic equations with the equations resulting from numerically integrating yields a fully determined set of algebraic equations that can be solved for .
Solving the system
At this point, we have a fully algebraic system of potentially non-linear equations (rewritten from the above two sets of equations):
to which we can apply a non-linear solver such as Newton's method. Begin the iteration with initial guess (the state of the circuit at the previous timestep), and iterate using until convergence, where is found by solving
where is the Jacobian of the system in (3). For particular values of , this is a well defined system of linear equations that can easily be solved with any of the standard methods.
Optimizing Newton's method for real time performance
To run the simulation in real time for non-trivial circuits requires significant optimization to the Newton's method iteration in the previous section. The most effective way to reduce the cost of Newton's method is to reduce the dimensionality of the problem. Our goal is to solve the system as much as possible at analysis-time, to minimize the dimensionality of the system to be solved at run-time during simulation.
To this end, observe that most of the equations in a system derived from a typical circuit are linear, even when being evaluated at analysis-time. We can rearrange the system such that we can solve for the linear solutions at analysis-time (as a symbolic expression of the non-linear solutions), leaving a lower rank system to be solved during simulation. To do this, rearrange the columns of the Jacobian matrix (and the corresponding rows of ) such that the entries of that contain non-linear terms appear in the last columns:
Row reduction gives:
To restate the observation more precisely, note that only the solutions for described by need to be solved for numerically during simulation. can be reduced to upper triangular form at analysis-time, so the corresponding solutions for can be directly solved for in terms of the non-linear solutions via back-substitution.
This optimization works by moving variables from an iterative solver with steps that are (Newton's method) to a closed form solution that is (back-substitution).
To quickly illustrate the potential of this optimization, I observed a 33x speedup on a particular amplifier circuit, with practically identical numerical results. On my machine, this brings the performance from well below real time to nearly 7 times faster than real time. In other words, without this optimization, the simulation could not be run in real time.
The naive run-time only approach to evaluating Newton's method will usually involve numerically robust techniques. For example, if Gaussian elimination is used to solve each iteration step, partial or full pivoting can be used.
We cannot be as careful when rearranging the system of equations during analysis for performance optimization, because selecting the largest pivots and selecting pivots corresponding to linear variables are competing goals. In addition, the candidate pivots are often symbolic expressions for which we cannot compute the magnitude. It is possible that by applying the rank reduction optimization to the Newton's method system at circuit analysis-time, Gaussian elimination uses a pivot that would not have been the maximum pivot found at runtime, resulting in a less numerically stable solution.
While this is a concern, I have not yet found a test circuit that produces a numerically unstable simulation when it should be stable. Unfortunately, I don't have much more I can say about this issue.
This is a relatively simple technique that works well for general circuits containing the usual set of components, and produces highly efficient numerical solutions that can be run in real time at audio sampling rates. It is probably also useful for other system simulation problems. I hope someone out there found this useful! Please don't hesitate to let me know if you find a mistake, or something is unclear.
The implementation of this method in LiveSPICE can be found in TransientSolution.cs.