Exposing SPICE's Limits: The Hidden Truth of .DISTO and B-Sources
The moment a perfect theory is defeated by an internal tool implementation.
A documentary of segfaults and silence when trying to make a simulator solve Taylor expansions.
1. The True Identity and Internal Calculations of .DISTO
Besides .SENS (sensitivity analysis), SPICE has another command that performs highly advanced mathematical calculations: .DISTO (distortion analysis).
Normally, this command is used in audio amplifiers and RF circuits. It calculates the magnitude of waveform "distortion," such as the second harmonic (noise at twice the original frequency) and third harmonic caused by the nonlinearity of components like transistors.
You might think running a transient analysis (.TRAN) and applying an FFT is enough to see waveform distortion. However, minute distortions get buried in the calculation error (noise floor). Instead of running a time-stepping simulation, .DISTO performs highly advanced matrix calculations internally. It analytically calculates the second and third derivatives of the nonlinear model at the current operating point and applies them to a Volterra series (a frequency-domain version of the Taylor expansion) to directly output theoretical values.
2. An Engineer's Inspiration: "Wait, can we use this for Taylor expansions?"
When I learned how .DISTO calculates internally, an idea suddenly struck me.
"If SPICE is internally calculating the second and third derivatives to output distortion, aren't those output values exactly the coefficients of a mathematical Taylor expansion (Maclaurin series)?"
When a function $f(x)$ is expanded using a Taylor series around $x=0$, it looks like this:
$$f(x) \approx f(0) + f'(0)x + \frac{f''(0)}{2!}x^2 + \frac{f'''(0)}{3!}x^3$$
The second harmonic (HD2) and third harmonic (HD3) output by .DISTO correspond exactly to the coefficients of $x^2$ and $x^3$. In other words, I realized that if you write an arbitrary formula in a B-source (nonlinear dependent source) and hit it with .DISTO, you would have an "ultimate mathematical solver" capable of instantly finding the Maclaurin expansion of multivariable functions, which is tedious to calculate by hand.
3. The Devil's Experiment: Taylor Expansion of $y = e^x$
The target is $y = e^x$, a beautiful function whose shape remains the same no matter how many times it is differentiated. To make SPICE calculate the coefficients of this Maclaurin expansion, I built the following netlist.
***** SPICE Taylor Expansion exp(x) ***** * Set the input for distortion analysis (DISTOF1=1) around x=0 Vx x 0 DC 0 AC 1 DISTOF1 1 * A nonlinear dependent "current source" calculating y = exp(x) By 0 y I = exp(V(x)) Ry y 0 1 * Calculate distortion (Taylor expansion coefficients) from 1Hz to 2Hz .DISTO LIN 2 1 2 .END
Theoretically, it should output a value equivalent to $1/2$ (0.5) for the second-order distortion and $1/6$ (approx. 0.166...) for the third-order distortion. However, these few lines of code ended up opening a Pandora's box for the historic simulator.
4. The Simulator's Scream: Explosions and Silence
When I fed this netlist into widely used open-source SPICE-derivative simulators, an unbelievable situation occurred.
Ngspice: An Unquestionable Fatal Error
The moment I executed run in Ngspice on Windows, the GUI left a cruel dialog box saying "Fatal error in NGSPICE," and the tool crashed entirely. A classic segfault.
MacSpice: A Silent All-Zero
On the other hand, MacSpice didn't crash and ran to the end. However, the output results were as follows:
Index frequency y ----------------------------------------------------- 0 1.00000e+00 ( 0.00000e+00, 0.00000e+00 ) 1 1.33333e+00 ( 0.00000e+00, 0.00000e+00 ) 2 1.66667e+00 ( 0.00000e+00, 0.00000e+00 )
It beautifully returned all zeros. Nothing was calculated.
5. Why Did This Happen? (The Reveal)
Why did the simulators collapse even though a perfect formula was provided? The answer lies in the limits of SPICE's internal implementation (C source code).
.DISTO only functions for genuine components (like diodes and transistors) whose formulas for second and third derivatives are hardcoded at the C language level in advance. The parser for "B-sources," where users can type arbitrary formulas, never had the functionality implemented to automatically derive higher-order derivatives.
Ngspice panicked when referring to a nonexistent memory area for higher-order derivatives (Null pointer) and self-destructed. MacSpice, on the other hand, played it safe by returning a zero since the feature didn't exist, managing to escape with a "Silent Failure".
6. Dr.WataWata's Insight
Even if a mathematical theory is perfect, it ultimately depends on the implementation status of the "person inside" the tool—the C code.
This is remarkably similar to a phenomenon (errata) in firmware development where "the microcontroller's datasheet says it can do it, but when you actually hit a register under special conditions, the peripheral hangs up."
Although the goal of making the simulator solve Taylor expansions was not achieved this time, it was a very satisfying verification result in the sense that the limits of the tool were literally uncovered "physically." Striking a black box to understand the limits of a system is precisely one of the great thrills of being an engineer.