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

— saturation current. The tiny reverse-bias leakage current, typically around A for a small silicon diode. It's small, but it sets the scale for the entire I-V curve. In the spice-rs DiodeModel, this is the is parameter (default: 1e-14).

— emission coefficient (ideality factor). A dimensionless number between 1 and 2. An ideal diode has ; real diodes have higher values because of recombination in the depletion region. Higher makes the exponential rise more gradual — the diode "turns on" at a slightly higher voltage.

— thermal voltage. Defined as , where is Boltzmann's constant, is temperature in Kelvin, and is the electron charge. At room temperature (300.15 K):

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 multiplies the current by .

The product appears so often that ngspice computes it once and calls it 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 sets the vertical scale, while the emission coefficient controls how sharply the diode turns on.

And on a logarithmic scale, the forward-bias region reveals the exponential relationship as a straight line:

Three regimes are visible:

Reverse bias (): Current is approximately . Essentially zero. The diode blocks.

Below turn-on ( V): Current is positive but negligibly small.

Forward conduction ( V): The exponential takes off. A 50 mV change produces a 7x change in current. This extreme sensitivity is both what makes diodes useful (as switches and rectifiers) and what makes them challenging to simulate.

Why the -1?

The equation has a inside the parentheses: . This ensures that at , the current is exactly zero. It also means that in reverse bias, as . In practice, the only matters when is very small or negative.

The exponential challenge

The factor in the exponent means that the argument to exp() grows at about 38.6 per volt (for ). At V (which can easily happen during a bad NR iteration), the exponent is 772 — and is astronomically larger than what a 64-bit float can represent.

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 the current guess for the diode voltage is . We evaluate the Shockley equation to get the current:

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 in parallel with a current source . In spice-rs, this is the stamping section of 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 (transconductance), (output conductance), and (body transconductance). More terminals and more partial derivatives, but the stamping mechanism is identical.


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 V, the exponent is 772 and 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:

And returns a limited voltage that is safe to feed into exp().

The critical voltage

The threshold is where limiting kicks in:

For typical values (, V, A):

In spice-rs, is precomputed during temperature setup:

// 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 and , the voltage change is compressed using a logarithm:

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 , no limiting is applied.

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 has V; a Schottky with has V.


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 is the zero-bias junction capacitance (typically 1-100 pF), is the junction potential (~0.7-0.9 V for silicon), and is the grading coefficient (0.5 for an abrupt junction, 0.33 for linearly graded).

As approaches , the capacitance would go to infinity. SPICE avoids this singularity by switching to a linear extrapolation above the threshold .

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 stored charge is proportional to the forward current:

The corresponding capacitance is:

Diffusion capacitance dominates in forward bias — at a forward current of 10 mA with ns, the diffusion capacitance is about 4 nF, orders of magnitude larger than the typical depletion capacitance.

Total junction capacitance

In reverse bias, , so depletion capacitance dominates. In forward bias, grows exponentially and overwhelms .

In spice-rs, the capacitance adds more conductance and current to the companion model via the ni_integrate call, which converts into an equivalent circuit element. For DC operating point, junction capacitance is irrelevant — nothing is changing, so .


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 () intersects the diode's I-V curve. Increasing shifts the load line right, increasing the diode current but barely changing the diode voltage (it stays near ~0.65V). Increasing makes the load line shallower, reducing current.


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 (), diodes D1 and D4 conduct. On the negative half-cycle, D3 and D2 conduct. The load sees a full-wave rectified signal — twice the frequency, half the ripple compared to the half-wave rectifier.

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:

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.