---
title: Simulating fields with FDTD
heading: Maxwell's equations stepped forward on a grid
eyebrow: C10 · Lesson 49 · Simulating fields with FDTD
xp: 40
---

::: hook
Closed-form answers run out quickly: a real antenna on a phone, a connector on a board, a radar reflection off an aircraft. Engineers solve those with field simulators, and one of the simplest and most widely used is the **finite-difference time-domain** method, FDTD. It divides space into a grid of tiny cells, stores E and H at staggered points, and steps Faraday's and Ampère's laws forward in time, a few lines of arithmetic per cell per step. Kane Yee published the scheme in 1966; the course's own wave pictures use it. Its two rules of thumb, a time step limited by the cell size and enough cells per wavelength, decide whether a simulation is right or wildly wrong.
:::

::: play
- On a grid, waves don't travel at exactly c: the discrete equations make them a little slow, more so for short wavelengths. The curve is the simulated speed over c against the number of cells per wavelength, N.
- S is the **Courant number**, $c\Delta t/\Delta z$. At S = 1 the one-dimensional scheme is exact. Lower S and the error appears: about 1% at 10 cells per wavelength, falling as $1/N^2$.
- Above S = 1 the scheme is unstable: errors grow without limit, which is why the time step is capped.
:::
:::

```sim plot
view: graph
x: N = 4..40
y: v = 0.85..1.01
slider: S = 0.1..1 = 0.5 step 0.01
plot: N/(pi*S)*asin(S*sin(pi/N)) | samples: 600 | label: simulated speed / c
plot: 1 | dashed
readout: speed error at 10 cells per wavelength, % = 100*(1 - 10/(pi*S)*asin(S*sin(pi/10))) | digits: 3
readout: at 20 cells, % = 100*(1 - 20/(pi*S)*asin(S*sin(pi/20))) | digits: 3
```

::: predict
## A 1D FDTD grid has 1 mm cells. What is the longest time step that stays stable?

- [ ] 1 ns
- [x] 3.3 ps
- [ ] 1 ps
- [ ] Any step works if the grid is fine enough

::: reveal
$\Delta t \le \Delta z/c = 10^{-3}/(3 \times 10^8) = 3.3$ ps. A wave must not cross more than one cell per step, or the update can't "see" it coming and the solution blows up. In 3D the limit is $\Delta/(c\sqrt3)$.
:::
:::

::: nameit
## The Yee scheme in one dimension

Store $E_x$ at whole-numbered grid points and times, and $H_y$ halfway between in both space and time. Faraday's and Ampère's laws become

$$H^{n+\frac12}_{k+\frac12} = H^{n-\frac12}_{k+\frac12} - \frac{\Delta t}{\mu\Delta z}\left(E^n_{k+1} - E^n_k\right)$$

$$E^{n+1}_k = E^n_k - \frac{\Delta t}{\varepsilon\Delta z}\left(H^{n+\frac12}_{k+\frac12} - H^{n+\frac12}_{k-\frac12}\right)$$

Each step updates every H from the E's beside it, then every E from the new H's. Materials enter through ε and μ cell by cell, so a dielectric slab is just a range of cells with a different ε.

## Stability

The **Courant number** $S = c\Delta t/\Delta z$ must satisfy S ≤ 1 in 1D (S ≤ $1/\sqrt3$ for cubic cells in 3D). Above that the scheme is unstable.

## Accuracy

Waves on the grid travel at

$$\frac{v}{c} = \frac{N}{\pi S}\sin^{-1}\left(S\sin\frac\pi N\right)$$

for N cells per wavelength: exact at S = 1 in 1D, otherwise slow by an error that falls as $1/N^2$. The usual rule is 10 to 20 cells per wavelength at the highest frequency of interest, and finer cells around small features.

## Boundaries, sources and cost

A metal wall is E = 0. An open boundary needs an absorbing layer (the **perfectly matched layer**, PML) so waves leave without reflecting. Sources are usually a Gaussian pulse added to E at a cell, which covers a whole band of frequencies in one run. A 3D simulation has $(L/\Delta)^3$ cells and needs about $L/(c\Delta t)$ steps per crossing, so halving the cell size costs 16 times the work.
:::

::: example
## A 2.4 GHz antenna is simulated with 20 cells per wavelength in 3D. What cell size and time step are needed?

::: solution
1. **Cell size.** λ = 125 mm, so
   $$\begin{aligned} \Delta &= \frac{125\ \text{mm}}{20} \\ &= 6.25\ \text{mm} \end{aligned}$$
2. **Time step.** The 3D Courant limit is
   $$\begin{aligned} \Delta t &= \frac{\Delta}{c\sqrt3} \\ &= \frac{6.25 \times 10^{-3}}{3 \times 10^8 \times 1.732}\ \text{s} \\ &= 12\ \text{ps} \end{aligned}$$

**Answer:** cells of 6.25 mm and a time step of 12 ps.
:::
:::

::: example
## A 1D run uses S = 0.5 and 10 cells per wavelength. How fast do its waves travel?

::: solution
1. **Numerical speed.**
   $$\frac{10}{0.5\pi}\sin^{-1}(0.5\sin 18^\circ) = 0.988$$
   so the waves run 1.2% slow.
2. **Over a long run.** After crossing 50 wavelengths the wave would lag by more than half a wavelength.
3. **Finer cells.** Doubling to 20 cells cuts the error to 0.3%.

**Answer:** the waves travel at 0.988 of their true speed, 1.2% slow.
:::
:::

::: example
## A 1D FDTD grid of 1000 cells uses 20 cells per wavelength at 1 GHz with a Courant number of 1. What are the cell size and time step, and how many steps does a pulse take to cross?

::: solution
1. **Cell size.** λ = 30 cm, so
   $$\begin{aligned} \Delta x &= \frac{30\ \text{cm}}{20} \\ &= 15\ \text{mm} \end{aligned}$$
2. **Time step.** At S = 1,
   $$\begin{aligned} \Delta t &= \frac{\Delta x}{c} \\ &= 50\ \text{ps} \end{aligned}$$
3. **Crossing time.** A pulse moves exactly one cell per step, so crossing 1000 cells takes 1000 steps, 50 ns.
4. **Why S = 1 is special.** In 1D, S = 1 is the magic step where the scheme has no dispersion at all.

**Answer:** Δx = 15 mm and Δt = 50 ps, and a pulse takes 1000 steps (50 ns) to cross.
:::
:::

::: check
given: dz = 0.1 | 1 | 5; S = 0.9 | 0.5
answer: S*dz*1e-3/299792458*1e12
unit: ps
tolerance: 0.5%

## A 1D grid has {dz} mm cells and runs at Courant number {S}. What time step is that?

::: right
Right. $S\Delta z/c$ = {answer} ps.
:::

::: misses
- [dz*1e-3/299792458*1e12] Include the Courant number S.
- [any] Not yet. $\Delta t = S\Delta z/c$.
:::
:::

::: check
given: d = 0.5 | 2 | 10
answer: d*1e-3/(299792458*sqrt(3))*1e12
unit: ps
tolerance: 0.5%

## What is the largest stable time step for a 3D grid of {d} mm cubic cells?

::: right
Right. $\Delta/(c\sqrt3)$ = {answer} ps.
:::

::: misses
- [d*1e-3/299792458*1e12] That's the 1D limit; in 3D it's smaller by $\sqrt3$.
- [any] Not yet. $\Delta/(c\sqrt3)$.
:::
:::

::: check
given: f = 1 | 5.8 | 28; N = 10 | 20; er = 2.2 | 4
answer: 299.792458/(f*sqrt(er))/N
unit: mm
tolerance: 0.5%

## A simulation must resolve {f} GHz with {N} cells per wavelength, inside a material with $\varepsilon_r$ = {er}. What cell size does that need?

::: right
Right. λ in the material over N: {answer} mm.
:::

::: misses
- [299.792458/f/N] The wavelength is shorter inside the material.
- [any] Not yet. $\lambda_0/(N\sqrt{\varepsilon_r})$.
:::
:::

::: check
given: N = 8 | 15 | 30; S = 0.3 | 0.7
answer: 100*(1 - N/(pi*S)*asin(S*sin(pi/N)))
unit: %
tolerance: 1%

## By what percentage are waves slow on a 1D grid with {N} cells per wavelength and Courant number {S}?

::: right
Right. {answer}% slow.
:::

::: misses
- [any] Not yet. $v/c = (N/\pi S)\sin^{-1}(S\sin(\pi/N))$.
:::
:::

::: check
kind: parts
given: L = 50 | 200; d = 1 | 2

## A 3D simulation covers a cube {L} mm on a side with {d} mm cells, storing six field components per cell as 8-byte numbers.

::: part
answer: (L/d)^3
tolerance: 0.5%

### How many cells?

::: misses
- [any] $(L/\Delta)^3$.
:::
:::

::: part
answer: (L/d)^3*6*8/1e6
unit: MB
tolerance: 0.5%

### How much memory do the fields need?

::: misses
- [any] Cells × 6 × 8 bytes.
:::
:::
:::

::: check
given: L = 100 | 500; d = 0.5 | 1; S = 0.5 | 0.8
answer: L/(S*d)
tolerance: 0.5%

## How many steps does a wave take to cross {L} mm on a 1D grid with {d} mm cells at Courant number {S}?

::: right
Right. $L/(c\Delta t) = L/(S\Delta)$ = {answer} steps.
:::

::: misses
- [L/d] Each step moves the wave only S cells.
- [any] Not yet. Distance over $c\Delta t$.
:::
:::

::: check
kind: choice

## A 1D FDTD run uses S = 1.2. What happens?

- [ ] It runs faster and slightly less accurately
- [x] It goes unstable: the fields grow without bound within a few hundred steps
- [ ] Waves travel at 1.2c
- [ ] Nothing: S only matters in 3D

::: misses
- [1] Above S = 1 there's no accuracy to trade: the scheme diverges.
- [3] The numbers blow up instead of traveling at any speed.
- [4] The 1D limit is S = 1.
:::
:::

::: check
kind: parts
given: f = 1 | 2.4 | 10; n = 10 | 20 | 40

## A 3D FDTD grid uses {n} cells per wavelength at {f} GHz.

::: part
answer: 299.8/f/n
unit: mm
tolerance: 0.5%

### What cell size is that?

::: misses
- [any] λ/n.
:::
:::

::: part
answer: 299.8/f/n*1e-3/(2.998e8*sqrt(3))*1e12
unit: ps
tolerance: 0.5%

### What is the largest stable time step?

::: misses
- [any] $\Delta x/(c\sqrt3)$ in 3D.
:::
:::
:::

::: check
kind: parts
given: N = 100 | 200 | 400

## A 3D FDTD grid is {N} cells on a side, with six field components in double precision.

::: part
answer: N^3*6*8/1e9
unit: GB
tolerance: 0.5%

### How much memory do the fields take?

::: misses
- [any] Cells × 6 × 8 bytes.
:::
:::

::: part
answer: 8
tolerance: 0

### By what factor does halving the cell size multiply the memory, for the same space?

::: misses
- [any] Twice as many cells along each of three axes.
:::
:::
:::

::: explain
## Why must the FDTD time step be short enough that a wave crosses at most one cell per step?

::: model
Each update computes a cell's new field from its immediate neighbors only, so information moves across the grid by at most one cell per step. If the real wave would cross more than one cell in a step, the numerical scheme can't keep up: the physics needs influence from cells it doesn't consult. Mathematically, the update then amplifies some wavelengths each step instead of preserving them, and rounding errors at those wavelengths grow exponentially until they swamp the solution. Keeping cΔt below the cell size (the Courant condition) guarantees the grid's speed limit is at least the physical one.
:::
:::

::: practice

::: check
given: dz = 0.1 | 0.25 | 0.5 | 1 | 2 | 5
answer: dz*1e-3/299792458*1e12
unit: ps
tolerance: 0.5%

## Largest 1D time step for {dz} mm cells?

::: misses
- [any] $\Delta z/c$.
:::
:::

::: check
given: f = 1 | 2.4 | 5 | 5.8 | 10 | 24; N = 10 | 15 | 20
answer: 299.792458/f/N
unit: mm
tolerance: 0.5%

## Cell size for {N} cells per wavelength at {f} GHz in air?

::: misses
- [any] λ/N.
:::
:::

::: check
given: L = 20 | 30 | 50 | 64 | 100 | 128; d = 1
answer: (L/d)^3
tolerance: 0.5%

## Cells in a {L} mm cube of {d} mm cells?

::: misses
- [any] $(L/\Delta)^3$.
:::
:::
:::
