# One Explicit Step

*Task 1 of 5 · [The Heat Equation & Stability](https://gpu.rocks/learn/heat-and-stability-514063bb.md) · GPU.js Learn*

The heat equation is the simplest interesting PDE there is:
`∂u/∂t = D·∇²u`. Every point drifts toward the average of what surrounds
it, at a rate set by the diffusivity `D`. Discretise the right-hand side
with the 5-point stencil — `left + right + up + down − 4·centre`, divided
by `dx²`, which Reaction–Diffusion derives in full — take a plain
forward-Euler step in time, and the whole solver is one line:

```js
u' = u + α·(left + right + up + down − 4u)
with  α = D·dt/dx²
```

That single dimensionless number `α`, the **diffusion
number**, is what this module is about. Collect the terms and the step turns
out not to be an addition at all. It is an *average*:

```js
u' = (1 − 4α)·u
     + α·left + α·right + α·up + α·down
```

Five weights that add to exactly one. Notice the shape of that centre weight,
`1 − 4α` — the next task is about what happens when it goes negative.

## Figures

- **five weights that add to one — while the middle one is positive**

## Goal

**Goal:** compute the diffusion number `α` from
`D`, `dt` and `dx`, and return one explicit step of
`field`.

## Requirements

- Compute `ALPHA = D * dt / (dx * dx)` in JavaScript — it reaches the kernel as a constant
- The kernel returns this cell’s new value: the old one plus `α` times the Laplacian
- The stencil and its wrap-around edges are already written — the world is a torus

## Hint 1 — dx is squared

`α = D·dt/dx²` — the cell spacing appears *squared*, because a
second derivative is a difference of differences. With `D = 8`,
`dt = 0.2` and `dx = 4` that comes to `0.1`.

## Hint 2 — the whole return

`lap` and `c` are already in scope, so the body is one line:

```js
return c + this.constants.alpha * lap;
```

## Same idea elsewhere

This three-line update is, almost character for character, the innermost loop of
every explicit finite-difference solver on every platform: a CUDA kernel with one thread
per cell, a WGSL compute shader reading and writing a storage texture, a Metal kernel
tiling the grid into threadgroups. What differs between them is memory layout and how the
halo is exchanged — never the arithmetic.

## Starter code

```js
// One forward-Euler step of the heat equation on a 48×48 torus.
const gpu = new GPU({ mode });

const D = 8;     // diffusivity
const dx = 4;    // cell spacing
const dt = 0.2;  // time step

// TODO: the diffusion number, alpha = D * dt / dx²
const ALPHA = 0;

const step = gpu.createKernel(function (u) {
  const x = this.thread.x;
  const y = this.thread.y;
  // the 5-point stencil, wrapped at the edges (a torus, as in Reaction–Diffusion)
  let xl = x - 1; if (xl < 0) xl = this.constants.size - 1;
  let xr = x + 1; if (xr > this.constants.size - 1) xr = 0;
  let yd = y - 1; if (yd < 0) yd = this.constants.size - 1;
  let yu = y + 1; if (yu > this.constants.size - 1) yu = 0;
  const c = u[y][x];
  const lap = u[y][xl] + u[y][xr] + u[yd][x] + u[yu][x] - 4 * c;
  // TODO: return the new value — the old one plus alpha times the Laplacian
  return c;
}, {
  output: [48, 48],
  constants: { size: 48, alpha: ALPHA },
});

const next = await step(field);
console.log('alpha:', ALPHA, ' centre weight 1 - 4*alpha:', 1 - 4 * ALPHA);
console.log('a hot cell on the block edge, was 1, is now:', next[24][20]);
```

---

Interactive version: https://gpu.rocks/learn/heat-and-stability-514063bb/1

[Next task](https://gpu.rocks/learn/heat-and-stability-514063bb/2.md)
