Heat Spread on a Grid
A live 2D heat solver on a 240 × 120 grid. Paint hot and cold spots, watch them spread, and learn why explicit heat solvers become unstable when the time step is too large.
Overview
This page simulates heat diffusion on a 240 × 120 grid in real time. Paint hot and cold spots, then watch the temperature differences smooth out as heat spreads. The grid’s edges are held at 0°C, so heat gradually drains toward the boundaries.
The solver uses FTCS (Forward Time, Centered Space), a simple, explicit method for solving the heat equation. It is fast and easy to follow, but its stability depends on the time-step size. If the numerical coefficient exceeds a fixed limit, some temperature patterns grow instead of fading.
Heat diffusion matters in many settings: how a chip distributes heat, how rock cools near a volcanic vent, and why cast iron can take time to warm yet retain heat. In each case, heat flows from warmer regions toward cooler ones, smoothing temperature differences over time.
Controls
- Paint mode: Hot or Cold.
- Brush temperature: 10–200, starting at 100.
- Brush radius: 1–8, starting at 3.
- Diffusivity α: 0.02–0.25, starting at 0.10.
- Steps per frame: 1–20, starting at 5.
- Clear grid: Reset the temperature field.
The brush paints a filled disc and stays within the grid. Mouse and touch input use pointer events; clicking other parts of the panel does not paint.
The simulation starts with two temperature spots already in place: a hot spot at 190°C with radius 14, centered at (0.38, 0.40), and a cold spot at −150°C with radius 11, centered at (0.66, 0.63).
Fourier’s Law and Thermal Resistance
Fourier’s law, published in 1822, relates heat flux to the temperature gradient and thermal conductivity :
The minus sign means heat flows down the temperature gradient, from hot to cold. Thermal conductivity measures how readily a material conducts heat.
For steady heat transfer through a flat layer, the same relationship can be written using thermal resistance:
This resembles Ohm’s law: temperature difference acts like voltage, heat-transfer rate like current, and thermal resistance like electrical resistance. For layers in series, their thermal resistances add.
Copper has a thermal conductivity of about 385 W/(m·K), while still air is around 0.026 W/(m·K). That is a difference of roughly 15,000 to 1, which helps explain why a trapped layer of air can provide effective insulation.
A wall and its insulation can be modeled as resistances in series:
For a fixed temperature difference, greater total resistance means a lower heat-transfer rate. In this simulation, the boundary temperature is pinned at 0°C. The boundaries act as ideal heat sinks that can absorb heat without warming up.
The Heat Equation
Combining Fourier’s law with conservation of energy gives the heat equation:
Here, is the thermal diffusivity, where is density and is specific heat capacity. Diffusivity measures how quickly temperature differences spread: conductivity promotes heat transfer, while the material’s heat capacity resists changes in temperature.
A material with high conductivity does not necessarily have the highest diffusivity, because density and heat capacity matter too. Copper has high diffusivity; cast iron generally has a lower value, so temperature changes spread more slowly through it.
Diffusion smooths sharp temperature differences over time. The characteristic distance heat spreads grows roughly as , rather than linearly with time. Unlike flowing air or water, diffusion does not carry heat along a bulk flow; it redistributes energy through neighboring material.
Real surfaces also exchange heat with their surroundings. Newton’s law of cooling models convective heat transfer as
where is the convective heat-transfer coefficient, is surface area, is surface temperature, and is the surrounding fluid temperature. Typical values vary widely: still air may be around 5–25 W/(m²·K), forced air around 25–250, and water cooling can reach roughly 100–20,000.
This page does not model convection at the surface. Instead, it fixes the grid’s edges at 0°C. That is an idealized boundary condition, not a model of cooling by air or water.
FTCS and the Stability Limit
FTCS advances time explicitly and approximates spatial derivatives using neighboring cells. With unit grid spacing and unit time steps, the update is
Each cell moves toward the average temperature of its four neighbors. The coefficient controls the size of that change. Because each new value depends only on the previous grid, the solver can update the entire field in one sweep using two buffers.
The method is explicit, but it is not stable for every choice of time step. To see why, consider a grid pattern with wave numbers and . The amplification factor for one step is
A stable update requires for every mode. The most restrictive mode is the checkerboard pattern, in which neighboring cells alternate between hot and cold. For ,
Requiring gives the two-dimensional stability limit
Above this limit, the checkerboard mode grows with each step, producing rapidly increasing hot and cold alternation. The resulting blow-up is a numerical instability, not a physical heat-flow effect.
The diffusivity control is capped at 0.25, the stability boundary. At exactly 0.25, the checkerboard mode has an amplification factor of −1, so that mode alternates without decaying. Values below the limit damp the mode. Because the current control does not exceed 0.25, it does not let you trigger the unstable regime directly; demonstrating blow-up interactively would require a separate test setting that permits values above the limit.
Implicit methods such as Crank–Nicolson and many alternating-direction implicit (ADI) schemes can avoid this explicit stability restriction for the heat equation, but require solving additional systems at each step. FTCS is useful here because its update is simple and its stability limit has a clear mathematical explanation.
Boundary Conditions
All four grid edges remain fixed at 0°C. The solver updates only interior cells, and the brush is restricted to the interior. The edge cells therefore retain their initial zero values.
Other boundary conditions are common in heat-transfer models:
- Fixed flux: Specifies the heat flow across a boundary. Zero flux represents an insulated edge.
- Convective cooling: Models heat exchange between a surface and a surrounding fluid using Newton’s law of cooling.
This simulation uses fixed-temperature boundaries instead. They act as ideal heat sinks, so the field gradually approaches 0°C if there are no internal heat sources. Repainting a hot spot adds heat to the field again.
How the Page Works
| Part | Implementation |
|---|---|
| Grid | 240 × 120 cells, with square cells at a 2 grid aspect ratio; two Float64Array buffers are swapped between steps |
| Solver | FTCS with dt = dx = 1; maximum |
| Boundaries | Fixed at 0°C; edge cells are not written by the solver |
| Rendering | An offscreen 240 × 120 canvas, scaled up with smoothing disabled |
| Painting | Pointer-based input; filled-disc brush, constrained to the interior |
| Colors | Black through red, yellow, and white for hot values; black through blue and cyan for cold values; display range clamped to ±200°C |
| Statistics | Maximum, minimum, and mean temperature, plus step count; calculated by scanning the grid |
| Initial state | Seeded hot and cold spots; simulation starts running on load |
The temperature field is rendered to a small offscreen canvas and scaled up with image smoothing disabled. Each grid cell maps to a square block of pixels, so the visualization matches the simulation grid without geometric distortion.
The solver and display run in the browser in a single tab. No WebGL, texture downloads, or external simulation service is needed.
What I Learned
Why can a time step that is too large produce a blinking grid?
The checkerboard pattern is a valid mode of the discrete update. When , its amplification factor has magnitude greater than one, so each step makes the pattern larger. Small numerical variations can grow into alternating hot and cold cells.
That is a stability failure of the discretization, not a claim that physical heat spontaneously oscillates. The current slider stops at the stability limit, so an interactive demonstration of blow-up requires a test-only way to exceed it.
Why does the heat fade without a visible cooler?
The edges are held at 0°C and act as permanent heat sinks. Temperature gradients drive heat toward those edges. With no internal source replenishing the field, the temperatures gradually approach the boundary value.
Paint a new hot spot and the diffusion process starts again.
What is the difference between thermal conductivity and diffusivity?
Thermal conductivity describes how much heat flows for a given temperature gradient. Thermal diffusivity,
describes how quickly temperature differences evolve. Conductivity favors heat transfer, while volumetric heat capacity measures how much energy is needed to change a material’s temperature.
This is why conductivity alone does not determine how quickly an entire object warms up or cools down.
Why does the convective heat-transfer coefficient vary so much?
The coefficient depends on the fluid, flow speed, geometry, and surface conditions. Still air often transfers heat slowly; forced air usually transfers it faster; moving water can transfer heat much more rapidly.
A useful way to think about the difference is that fluid motion changes how effectively heat is carried away from the surface. The fixed-temperature boundary in this simulation is an idealization: it keeps the edge pinned at its specified value rather than calculating a finite convective heat-transfer rate.
Why does thermal radiation become important at high temperatures?
The emitted thermal-radiation power of an idealized gray surface scales with the fourth power of its absolute temperature:
Here, is the Stefan–Boltzmann constant, is emissivity, is surface area, and must be measured in kelvin. Net radiation exchange with the surroundings depends on their temperature as well.
Radiation is often modest at room temperature but can become a dominant heat-transfer mechanism at high temperatures, depending on the surface and surrounding conditions. In a vacuum, convection is absent, so radiation and heat conduction through physical connections become especially important. This solver omits radiation because it focuses on diffusion within a two-dimensional field.
Tried Shortcut (Not Shipped)
I tried replacing the numerical solver with a learned shortcut: a small MLP model that maps painted heat sources directly to a steady-state temperature field.
The model returns a prediction in 4.9 ms, compared with 63.9 ms for the solver, but its mean relative L2 error is 0.361 and its 95th-percentile error is 0.620. That accuracy is not good enough for a teaching tool where the displayed temperature field should be trustworthy.
A patch-based version with 32 pixels of surrounding context also failed to represent the global dependencies of the steady-state solution. The repository includes the weights, evaluation results, and worst-case example. The model is not used by the interactive page, and these performance figures come from that separate repository rather than from the widget itself.
Honest Limits
This is a flat, two-dimensional diffusion model on a 240 × 120 grid with fixed 0°C boundaries. It does not model fluid flow, convective surface cooling, or thermal radiation. The FTCS stability limit is ; the current control is capped at that limit, so the unstable regime is explained mathematically but cannot be reached through the normal slider.
The page is intended to teach heat diffusion and numerical stability, not to predict real component temperatures or model industrial heat treatment.
References
- Boltzmann, Ludwig. 1884. “Ableitung des Stefan’schen Gesetzes, betreffend die Abhängigkeit der Wärmestrahlung von der Temperatur aus der electromagnetischen Lichttheorie.” Annalen der Physik 258 (6): 291–294. https://doi.org/10.1002/andp.18842580616
- diolei. n.d. “nanotherm — Unshipped MLP Shortcut.” GitHub repository. Accessed October 5, 2026. https://github.com/diolei/nanotherm
- Incropera, Frank P., David P. DeWitt, Theodore L. Bergman, and Adrienne S. Lavine. 2007. Fundamentals of Heat and Mass Transfer. 6th ed. Wiley.
- Press, William H., Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery. 2007. Numerical Recipes: The Art of Scientific Computing. 3rd ed. Cambridge University Press.
- Stefan, Josef. 1879. “Über die Beziehung zwischen der Wärmestrahlung und der Temperatur.” Sitzungsberichte der Kaiserlichen Akademie der Wissenschaften: Mathematisch-Naturwissenschaftliche Classe 79: 391–428.
- Wikimedia Foundation. 2026. “Heat Equation.” Accessed October 5, 2026. https://en.wikipedia.org/wiki/Heat_equation