A one-file starter: a double layer charging after a voltage step

A blocking electrode in 0.1 M NaCl, 30 Debye lengths (29 nm) from the bulk solution, stepped by ψ at t = 0 and followed in time: Poisson's equation with both ions' drift and diffusion (time-dependent Poisson–Nernst–Planck), in the browser. No ion crosses the electrode, yet a current flows until the layer has charged. At the first instant the ions haven't moved, so the cell is a resistor and φ falls across it in a straight line; then the ions gather to screen the electrode, and the drop collects into the few Debye lengths next to it (top, against where it ends up, dashed). The electrode's charge (below) climbs as a capacitor charging through a resistor would, until the step is big enough for the layer to start drawing salt out of the solution. It plays in log time, 0.01 τ to 1000 τ. The page loads driftlet from a pinned CDN URL, with no install and no build step.

Checks before you build on it

After a 5 mV step, the charge still to come falls as e^(−t/τ) from τ to 3 τ, with τ within 0.2% of λL/D̄ = 16 ns, the RC time of Bazant, Thornton and Ajdari (Phys. Rev. E 70, 021506, 2004): the bulk's resistance L/κ times the diffuse layer's capacitance ε/λ, with D̄ the mean of the two ions' diffusivities. Every step settles on the steady state, whose charge is Gouy–Chapman's to 0.04%. A bigger step is slower: 63% of the charge comes at 2.6 τ for 0.1 V and 32 τ for 0.2 V. The layer's capacitance grows as cosh(Fψ/2RT), 25-fold at 0.2 V, and as it fills it takes in salt, which has to diffuse back from the bulk: at 0.2 V the salt five Debye lengths out is down to 43% of the bulk's at 10 τ. The first instant carries a small jump, the whole cell's capacitance ε/L in series with the layer's, and φ halfway across is still 0.49 ψ at 0.01 τ.

The whole page

<!doctype html>
<meta charset="utf-8">
<title>double layer charging</title>
<label>step ψ <select id="psi"><option value="0.005">5 mV</option><option value="0.05">50 mV</option><option value="0.1" selected>0.1 V</option><option value="0.2">0.2 V</option><option value="0.25">0.25 V</option></select></label>
<button id="again">play again</button>
<div id="readout"></div>
<div id="figure"></div>
<script type="module">
import { Device, EPS0, FARADAY, GAS_CONSTANT } from 'https://cdn.jsdelivr.net/npm/driftlet@1.23.0/src/index.js';
import { build, layer, bath, aqueous, WATER_EPSR } from 'https://cdn.jsdelivr.net/npm/driftlet@1.23.0/src/kit.js';

// 0.1 M NaCl between a blocking electrode and the bulk solution 30 Debye lengths away (Poisson with
// both ions' Nernst–Planck transport). At t = 0 the electrode steps by ψ from the solution's own potential.
const c = 100; // mol/m³
const RT = GAS_CONSTANT * 298.15, eps = WATER_EPSR * EPS0;
const debye = Math.sqrt((eps * RT) / (2 * FARADAY * FARADAY * c)); // 0.96 nm
const L = 30 * debye;
const water = aqueous(['Na+', 'Cl-']);
const { 'Na+': na, 'Cl-': cl } = water.materials.water.species;
const tau = (debye * L) / ((na.D + cl.D) / 2); // the RC time, λL/D̄: the bulk's resistance times the layer's capacitance (16 ns)
const device = new Device(build({
  library: [water],
  stack: [
    { V: 0, phi: 'pinned', zeroCharge: 0 }, // the electrode: φ at its face is its V, and no ion crosses
    layer('water', L),
    bath({ 'Na+': c, 'Cl-': c }, 'Cl-'), // the bulk solution
  ],
  grid: { hmin: debye / 1e4, hmax: debye / 3, ratio: 1.08 },
}));
const bulk = device.solve().phi.at(-1); // the solution's own potential, which ψ is measured from

// From rest, step the electrode to ψ: returns the clock's start, the final state and its charge to reach.
function stepTo(psi) {
  device.set({ contacts: { left: { V: bulk + psi } } });
  const final = device.solve(); // where it ends up: the steady state at ψ
  device.set({ contacts: { left: { V: bulk } } });
  const rest = device.solve(); // back to rest, and then the step
  device.set({ contacts: { left: { V: bulk + psi } } });
  return { t0: rest.time, final, Q: final.contacts.left.D - rest.contacts.left.D, D0: rest.contacts.left.D };
}
const gouyChapman = (psi) => Math.sqrt(8 * eps * RT * c) * Math.sinh((FARADAY * psi) / (2 * RT));

// The page: φ across the cell now (against the final profile, dashed), and the electrode's charge in time.
const times = Array.from({ length: 126 }, (_, k) => tau * 10 ** (-2 + k / 25)); // 0.01 τ to 1000 τ, evenly in log t
function plots(sol, step, psi, seen) {
  const W = 640, H = 230, X = (x) => 60 + (x / L) * 560, Y = (u) => 20 + (1 - u) * 170;
  const phi = (s, dash) => `<polyline fill="none" stroke="#2471a3" stroke-width="2"${dash ? ' stroke-dasharray="5 4" opacity="0.6"' : ''} points="${Array.from(s.x, (x, k) => `${X(x).toFixed(1)},${Y((s.phi[k] - bulk) / psi).toFixed(1)}`).join(' ')}"/>`;
  const xs = [0, 5, 10, 15, 20, 25, 30].map((k) => `<text x="${X(k * debye)}" y="${H - 18}" text-anchor="middle" font-size="12">${k}</text>`).join('');
  const ys = [0, 0.5, 1].map((u) => `<text x="54" y="${Y(u) + 4}" text-anchor="end" font-size="12">${u}</text><line x1="60" x2="620" y1="${Y(u)}" y2="${Y(u)}" stroke="#ddd"/>`).join('');
  const top = `<svg viewBox="0 0 ${W} ${H}" width="${W}" height="${H}" font-family="sans-serif">${ys}${xs}<text x="340" y="${H - 2}" text-anchor="middle" font-size="12">distance from the electrode (Debye lengths)</text><text x="70" y="14" font-size="12">(φ − φ_bulk)/ψ</text>${phi(step.final, true)}${phi(sol, false)}</svg>`;
  const T = (t) => 60 + ((Math.log10(t / tau) + 2) / 5) * 560;
  const curve = (pts, attrs) => `<polyline fill="none" ${attrs} points="${pts.map(([t, u]) => `${T(t).toFixed(1)},${Y(u).toFixed(1)}`).join(' ')}"/>`;
  const ts = [0.01, 0.1, 1, 10, 100, 1000].map((f) => `<text x="${T(f * tau)}" y="${H - 18}" text-anchor="middle" font-size="12">${f}</text>`).join('');
  const rc = curve(times.map((t) => [t, 1 - Math.exp(-t / tau)]), 'stroke="#888" stroke-width="1.5" stroke-dasharray="5 4"');
  const bottom = `<svg viewBox="0 0 ${W} ${H}" width="${W}" height="${H}" font-family="sans-serif">${ys}${ts}<text x="340" y="${H - 2}" text-anchor="middle" font-size="12">time since the step (RC times, τ = λL/D̄ = ${(tau * 1e9).toFixed(0)} ns)</text><text x="70" y="14" font-size="12">electrode charge / its final value</text><text x="600" y="${Y(0.15)}" text-anchor="end" font-size="12" fill="#888">1 − e^(−t/τ)</text>${rc}${curve(seen, 'stroke="#c0392b" stroke-width="2"')}</svg>`;
  return top + bottom;
}
let playing = 0;
function play() {
  const run = ++playing, psi = +document.getElementById('psi').value, step = stepTo(psi), seen = [];
  let k = 0;
  const frame = () => {
    if (run !== playing) return; // a newer step took over
    const sol = device.advance(step.t0 + times[k], { budgetMs: 12, tol: 1e-5 });
    if (!sol.converged) return void (document.getElementById('readout').textContent = 'no solution here');
    const q = (sol.contacts.left.D - step.D0) / step.Q, t = sol.time - step.t0;
    if (t > 0) seen.push([t, q]);
    if (sol.done) k++;
    document.getElementById('readout').textContent = `ψ = ${(psi * 1000).toFixed(0)} mV, t = ${t < 100 * tau ? (t / tau).toPrecision(3) : (t / tau).toFixed(0)} τ: electrode charge ${(q * 100).toFixed(1)}% of ${step.Q.toPrecision(4)} C/m² (Gouy–Chapman: ${gouyChapman(psi).toPrecision(4)})`;
    document.getElementById('figure').innerHTML = plots(sol, step, psi, seen);
    if (k < times.length) requestAnimationFrame(frame);
  };
  requestAnimationFrame(frame);
}
document.getElementById('psi').addEventListener('change', play);
document.getElementById('again').addEventListener('click', play);
play();
</script>

Next: the double layer starter drags the potential and solves the steady state at each; the steps demo puts a reaction at the electrode, where the current no longer stops; and the guide for coding agents has templates for the rest.