
driftlet simulates charged species moving through one-dimensional devices: semiconductor
junctions and solar cells, electrochemical cells and batteries, double layers, membranes and
nerves. It's a JavaScript library with no dependencies, fast enough to solve live in a
web page as you drag a slider.
Live demos · one-file starter · npm ·
reading level diagrams · device reference ·
agent guide · changelog
One kind of physics. Those look like different subjects, but underneath they're the same
one. Each is a cast of species (electrons, holes, ions, neutral molecules) that drift and
diffuse, each down its own electrochemical potential; their charge sets the electric field,
reactions turn one into another, and at interfaces they pass from one material to the next or
are stopped. A pn junction is electrons and holes in silicon. A silver electrode is Ag⁺ and NO₃⁻
in water, with Ag⁺ trading places with the metal's electrons at its surface. A nerve is Na⁺ and
K⁺ on either side of a membrane, crossing it through channels that open with voltage, and the
impulse travels along the fibre on the current those same ions carry inside it. Even the levels
each field draws are one quantity: a quasi-Fermi level, an electrode potential and a Nernst
potential are each a species' electrochemical potential. driftlet solves that one problem, in
steady state, in time and as small-signal impedance, so a new field is mostly a new cast, and
the work that makes it reliable in one field makes it reliable in the others.
Built to be relied on. A drift–diffusion solver that handles a textbook pn junction is a
weekend's work; one that keeps working on the devices people actually build is not. In real
devices concentrations span 30 orders of magnitude and double layers are a millionth of the
device's length, and there a plain Newton solver fails or, worse, quietly converges to a wrong
answer. driftlet is tested two ways: every physics feature against an analytic result or an independent code, and
thousands of randomly generated devices (junctions, MOS capacitors, solar cells, electrolyte
cells, electrodes) put through cold solves, sweeps, transients and impedance, every result
checked. A solve that fails reports it, with what went wrong where it can tell.
How far to trust it has the details, the numbers and the known limits.
Status: 1.0. The API is stable: a change that breaks working code comes only with a major
version.
Install
npm install driftlet
import { Device, units } from 'driftlet'; // the solver
import { build, layer, ohmic } from 'driftlet/kit'; // helpers that write definitions, data, live demos
import { bandDiagram } from 'driftlet/plot'; // a level diagram as an SVG string
Or straight from a CDN in a page, pinned to a version:
https://cdn.jsdelivr.net/npm/driftlet@1.23.0/src/index.js (and src/kit.js, src/plot.js).
TypeScript declarations ship in the package. Whole pages to copy, running, each in one file:
a pn junction with a bias slider,
an electrode's double layer, with ions that take space,
that double layer charging after a voltage step, and
a solar cell's I–V curve. Writing a demo with an LLM
agent? Point it at llms.txt.
A semiconductor junction
A silicon pn junction with ohmic contacts, at equilibrium and then under forward bias. A device
is plain data; build() from driftlet/kit writes it from the stack as it's drawn, with a
data library for the material:
import { Device, units } from 'driftlet';
import { build, layer, ohmic, semiconductor } from 'driftlet/kit';
const dev = new Device(
build({
T: 300,
library: [semiconductor('Si')], // e⁻ and h⁺ with Sze's band data at 300 K
stack: [
ohmic(0), // e⁻ and h⁺ in equilibrium with a metal, the terminal at 0 V
layer('Si', units.um(2), { name: 'n', donors: units.perCm3(1e17) }),
layer('Si', units.um(2), { name: 'p', acceptors: units.perCm3(1e16) }),
ohmic(0),
],
grid: { hmin: units.nm(0.5), hmax: units.nm(20) },
}),
);
const eq = dev.solve();
console.log('built-in potential (V):', eq.phi[0] - eq.phi[eq.phi.length - 1]); // => 0.7947
dev.set({ contacts: { right: { V: 0.4 } } }); // p side at +0.4 V: forward bias
const on = dev.solve(); // warm-started from the previous solution
console.log('current (A/m²):', on.current); // negative: it flows toward −x, from p to n
An electrochemical cell
Silver nitrate between two silver electrodes, as a macroscopic (strictly neutral) electrolyte.
A reversible electrode is a contact whose terminal species is the ion it exchanges: Ag⁺ in
equilibrium with the metal, NO₃⁻ blocked. Polarized, the cell's current saturates at the
limiting current, i_lim = 4FD₊c₀/L for this binary salt:
import { Device, FARADAY } from 'driftlet';
import { build, layer, aqueous } from 'driftlet/kit';
const L = 100e-6, c0 = 10; // m, mol/m³ (10 mM)
// Ag⁺ + e⁻ ⇌ Ag at the metal: V is the voltage of Ag⁺ there, i.e. the electrode potential.
const silver = (V) => ({ V, terminal: 'Ag+', species: { 'Ag+': 'equilibrium', 'NO3-': 'blocked' }, phi: 'bulk' });
const dev = new Device(
build({
library: [aqueous(['Ag+', 'NO3-'], { epsr: 0 })], // epsr 0: no double layers resolved
// Ag⁺ reaches the contacts and takes their level; NO₃⁻ doesn't, so it needs its amount.
stack: [silver(0), layer('water', L, { c0: { 'NO3-': c0 } }), silver(0)],
grid: { hmin: 10e-9, hmax: 0.5e-6 }, // graded: fine where Ag⁺ depletes at the electrode
}),
);
const iLim = (4 * FARADAY * 1.648e-9 * c0) / L;
for (const V of [0.05, 0.1, 0.2, 0.4]) {
dev.set({ contacts: { right: { V } } });
const sol = dev.solve();
// V > 0 raises Ag⁺'s level on the right: silver dissolves there and plates on the left, so the
// current flows toward −x.
console.log(`${V} V: I/i_lim = ${(-sol.current / iLim).toFixed(3)}`); // => 0.451, 0.750, 0.960, 0.999: tanh(V/4V_T)
}
Solutions are plain objects of Float64Arrays, ready to plot (fresh arrays from every solve):
x, phi, and per species c, mu (electrochemical potential μ̄), muStd (standard level)
and the voltage views V, Vstd. They also carry the terminals' voltages and currents, fluxes,
conservation bookkeeping and warnings; see the device reference. The
kit adds reactions written as equations ('Ag+ + e- = Ag(s)'), redox levels,
level diagrams, slider demos, a transient recorder, describe() (which flags likely unit slips),
check() (a solution's ledgers and a grid-convergence check),
and curated devices with reference results: IonMonger's perovskite cell, with its J–V scans.
Demos
Live pages in demos/, each building its device with driftlet/kit, solving it in the
browser as you move the controls, and showing its whole script as a worked example. Most draw the
closed-form theory alongside, as a check. They're live at
marklundeberg.com/driftlet/demos; to run
them locally, serve the repository root (e.g. python3 -m http.server) and open /demos/.
|
|
|
|
|
|
| pn junction: quasi-Fermi levels under bias; I–V against Shockley |
Solar cell: Jsc and Voc against collection theory |
MOS capacitor: band bending; C–V at any frequency, low to high |
|
|
|
| Cyclic voltammetry: a Fermi level against a redox level |
Daniell cell: a real salt bridge, leaking, ion by ion |
Saturation: Ag | AgNO₃ | Ag up to its limiting current, and a thin-film transistor pinching off, against the charge-sheet model |
|
|
|
| Impedance: the Warburg arc, and the cell's small-signal response inside |
Ion-exchange membrane: Donnan steps, ion by ion, against TMS theory |
Double layer: dilute vs crowded ions, against closed forms |
|
|
|
| Intercalation host: cycling between cutoffs against the OCV |
Organic solar cell: excitons diffusing to a donor/acceptor interface, against theory |
Perovskite hysteresis: mobile ions and scan rate, against IonMonger |
|
|
|
| Charged nanochannel: selectivity, overlapping Donnan layers and rectification, against Teorell–Meyer–Sievers |
Resting potential: leaks and the Na⁺/K⁺ pump, against Mullins–Noda; the run-down to Donnan |
Liquid junctions: a patch pipette's against Henderson, Planck and LJPcalc; a charged frit |
|
|
|
| Action potential: Hodgkin and Huxley's gated channels, each ion's driving force through the spike, against the HH equations |
Propagation: a spike travelling along an axon at HH's speed, out of the ions' drift; which ions carry it |
Myelin: saltatory conduction, node to node, against a compartmental cable; a race with a bare axon |
Is it for your problem?
The same equations go by different names in different fields. If your problem is one-dimensional
(planar, or radial: spheres and cylinders, or any cross-section varying along x), it's probably here: