The Diode
The diode is the simplest nonlinear device in SPICE, and therefore the best place to understand how all nonlinear devices work.
A diode does one thing: it lets current flow easily in one direction and blocks it in the other. A silicon diode forward-biased at 0.65V might pass milliamps of current. Reverse the polarity and it passes essentially nothing — a few picoamps of leakage. This asymmetry, described by an exponential equation, is what makes the diode nonlinear and what makes it interesting for simulation.
Every technique introduced here — linearization, companion models, voltage limiting — applies directly to MOSFETs, BJTs, and every other semiconductor device in SPICE. The diode just makes them easier to see.
The Shockley equation
The current through an ideal diode is:
This is the Shockley diode equation, and it governs every PN junction in SPICE — not just standalone diodes, but also the junctions inside MOSFETs and BJTs.
The parameters
DiodeModel, this is the is parameter (default: 1e-14).
The thermal voltage is not a model parameter — it's a physical constant that depends only on temperature. At room temperature, every 26 mV increase in
The product vte:
// From device/diode.rs
let vt = BOLTZMANN_OVER_Q * self.temp; // kT/q
let vte = self.model.n * vt; // n * kT/q
Interactive I-V curve
Adjust the diode parameters to see how the Shockley equation shapes the I-V curve. The saturation current
And on a logarithmic scale, the forward-bias region reveals the exponential relationship as a straight line:
Three regimes are visible:
Reverse bias (
Below turn-on (
Forward conduction (
Why the -1?
The equation has a
The exponential challenge
The factor exp() grows at about 38.6 per volt (for
SPICE handles this with voltage limiting (below). But the exponential sensitivity is worth internalizing: it's the root cause of most diode convergence problems, and the same issue appears in every device with a PN junction.
Linearization and companion models
At each Newton-Raphson iteration, every nonlinear device must be replaced by a linear equivalent that can be stamped into the MNA matrix. For the diode, this means turning the exponential I-V curve into a straight line — a companion model consisting of a conductance in parallel with a current source.
The tangent-line approximation
Suppose we're at iteration
and take its derivative to get the conductance (slope of the I-V curve at this point):
These two numbers define a tangent line to the I-V curve. It can be rearranged into a conductance in parallel with a current source:
The companion model stamps into the matrix exactly like a resistor plus a current source:
Visualizing the tangent line
Drag the operating point along the I-V curve. The dashed line is the companion model (tangent line) — the linear approximation the solver uses at that iteration.
The tangent line crosses the true I-V curve at the operating point. To the left, it overestimates the current (the curve is concave up). To the right, it underestimates. This systematic error is what Newton-Raphson corrects by re-linearizing at each iteration.
The companion circuit
The companion model is a Norton equivalent: a conductance device/diode.rs:
// From device/diode.rs — load() function
let cdeq = cd - gd * vd; // Norton equivalent current
// RHS stamps
mna.stamp_rhs(n, cdeq); // positive at cathode
mna.stamp_rhs(pp, -cdeq); // negative at anode
// Conductance stamps
mna.stamp(pp, pp, gd);
mna.stamp(n, n, gd);
mna.stamp(n, pp, -gd);
mna.stamp(pp, n, -gd);
A critical detail: every device does this
The diode companion model is not specific to diodes. Every nonlinear device in SPICE follows exactly the same pattern: evaluate the device equations, compute the conductance (partial derivative), compute the Norton equivalent current source, and stamp both into the matrix.
For a MOSFET, the "conductances" include
Voltage limiting
Newton-Raphson proposes new voltages by solving a linearized system. The linearization is only accurate near the current operating point, so the proposed voltage might be far from the truth — and for a diode, "far from the truth" can mean catastrophe.
If the solver proposes exp() returns infinity. The simulation crashes.
Voltage limiting prevents this by clamping the proposed voltage change before the exponential is evaluated.
The pnjlim algorithm
The limiting function in SPICE is called pnjlim (PN junction limit). It takes four inputs:
vnew— the voltage proposed by the solvervold— the voltage from the previous iterationvt— the thermal voltage () vcrit— a critical voltage threshold
And returns a limited voltage that is safe to feed into exp().
The critical voltage
The threshold
For typical values (
In spice-rs,
// From device/diode.rs — temperature()
self.t_vcrit = vte * (vte / (SQRT_2 * self.t_sat_cur)).ln();
The limiting rules
Case 1: Large positive voltage, big change. If
A proposed jump of 5V becomes a step of maybe 0.1V. The solver's intent is preserved (increase the voltage) but the magnitude is tamed.
Case 2: Negative voltage, big swing. The voltage is clamped to prevent wild swings into deep reverse bias.
Case 3: Normal region. If the change is small or the voltage is below
The check flag
When limiting is applied, pnjlim sets check = true. This flag propagates up to the NR loop as the noncon flag. Its meaning: "I had to override the solver's voltage, so the solution isn't self-consistent yet — don't declare convergence."
Why not just use smaller steps?
Two reasons: (1) Only exponential devices need it — limiting resistors and capacitors would slow convergence for no benefit. (2) The limit depends on the device — a diode with
Junction capacitance
For DC operating point analysis, the diode is fully described by its I-V curve. But for transient analysis (circuits with changing signals) and AC analysis (small-signal frequency response), the diode also stores charge. This charge storage shows up as junction capacitance.
Depletion capacitance
When a diode is reverse-biased, the depletion region behaves like a parallel-plate capacitor. The SPICE model:
where
As
The charge:
Diffusion capacitance
When forward-biased, minority carriers are injected across the junction. They take a finite time to recombine — the transit time,
The corresponding capacitance is:
Diffusion capacitance dominates in forward bias — at a forward current of 10 mA with
Total junction capacitance
In reverse bias,
In spice-rs, the capacitance adds more conductance and current to the companion model via the ni_integrate call, which converts
Circuits: the diode-resistor network
Here is the basic diode-resistor circuit, simulated end to end. Adjust the supply voltage and resistance to see how the operating point shifts.
The operating point is where the resistor's load line (
Circuits: rectifiers
The diode's ability to conduct in only one direction makes it the core component of rectifiers — circuits that convert AC to DC.
Half-wave rectifier
The simplest rectifier: a diode in series with the load.
During the positive half-cycle, the input voltage rises above the output voltage plus the diode's forward drop (~0.65V). The diode conducts, charging the capacitor and delivering current to the load.
During the negative half-cycle, the input drops below the output. The diode is reverse-biased and blocks. The capacitor slowly discharges through the load resistor, holding the output near its peak value.
The result is a DC output with ripple — small periodic dips as the capacitor discharges between conduction pulses.
Here is what the half-wave rectifier waveforms look like:
The diode "costs" about 0.65V: the peak output is about 4.35V instead of 5V. With a real diode model that includes series resistance (RS), the drop is slightly higher under load.
Full-wave bridge rectifier
A bridge rectifier uses four diodes to capture both half-cycles:
On the positive half-cycle (
Two diode drops are in the path (about 1.3V total), so the peak output from a 12V peak source is about 10.7V.
What to observe
When studying these simulations, look for:
- Ripple voltage — the periodic dip in
between conduction pulses. Larger capacitance means less ripple. - Diode current pulses — the diode conducts only near the peaks of the input waveform, in brief, high-current pulses. The peak diode current is much higher than the DC load current.
- Forward voltage drop — the ~0.65V offset between input peak and output peak. It increases slightly with current due to series resistance.
- Reverse recovery — if the diode model includes transit time (TT), there's a brief moment when the diode continues conducting after the voltage reverses, as the stored minority-carrier charge is swept out.
Bridge rectifiers are interesting for simulation because they have rapid diode switching events. Twice per cycle, two diodes turn off and two turn on. At each switching event, the NR iteration must track the transition from forward conduction to reverse blocking. The series resistance RS creates an internal node inside each diode, which adds unknowns to the matrix but improves convergence by limiting how quickly the junction voltage can change.
SPICE Rectifier Simulation
The waveforms above use a simplified JavaScript model of the diode. Now let's run the actual SPICE simulator on a half-wave rectifier with a filter capacitor. Every diode evaluation uses the full Shockley equation, Newton-Raphson converges the nonlinear system at each timestep, and the trapezoidal rule integrates the capacitor charge.
The SPICE result captures effects the simplified model misses: the exact forward drop depends on the current (not a fixed 0.65V), the capacitor charges through the diode's dynamic resistance, and the simulator's adaptive timestep concentrates effort around the conduction pulses where the diode switches rapidly between forward and reverse bias.
Try increasing the capacitance — the ripple drops because the RC discharge time constant grows. Increasing the load resistance has the same effect (less current drawn from the capacitor between conduction pulses). Increasing frequency also reduces ripple because the capacitor has less time to discharge between peaks.