C10 · Lesson 50 · Electromagnetics lab
A one-dimensional FDTD simulator in Python
In this lab you'll build the simulator from lesson 49 and use it as a virtual test bench. A few lines of NumPy step Maxwell's equations across a thousand cells; a Gaussian pulse launched into the grid travels, meets a block of dielectric, and splits into a reflected and a transmitted pulse. Measuring them gives the reflection coefficient, which you can compare with the formula from lesson 31, and timing the pulse inside the dielectric gives its speed. When the simulation and the theory agree to a percent, you have a tool you can trust on problems that have no formula.
- The analytic version of what your simulator will show: a pulse arrives from the left at a dielectric starting at z = 0. Drag t to watch it hit.
- After the hit, a reflected pulse of Γ times the height heads back (inverted, for a denser medium), and a transmitted pulse of τ times the height continues, narrower and slower by the refractive index n.
- Change and watch the split change. Your FDTD run should reproduce Γ to about 1%.
Predict
A pulse in vacuum hits a dielectric with = 4. What is the transmitted pulse's peak, relative to the incident one?
0.67: τ = 1 + Γ = 1 − 1/3 = 2/3. The transmitted pulse carries less field, but in a medium with lower impedance, so its power is of the incident, and the reflected 1/9 makes up the rest.
Name it
The simulator
In normalized units (c = 1, cells of size 1, fields scaled so that E and H have the same size in a vacuum wave), one Yee step with Courant number S is
H[k] += S * (E[k+1] - E[k]) for k = 0 .. N-2
E[k] += S / eps[k] * (H[k] - H[k-1]) for k = 1 .. N-2
leaving E[0] and E[N−1] at zero (metal walls). In NumPy each line is one slice operation. A soft source adds a Gaussian to E at one cell each step, , which launches a pulse each way; recording E at a probe cell gives a time trace.
Measuring
- Reflection: put the dielectric's edge to the right of the source and a probe between them. The probe sees the incident pulse pass, then the reflection come back; Γ is the ratio of their peaks (with sign).
- Speed: fill the grid with the dielectric, and time the pulse's peak between two probes a known distance apart: speed = distance / steps × S, in units of c.
Make the grid long enough that pulses bouncing off the end walls don't reach the probes during the measurement.
Worked example
The probe sees an incident peak of 0.98 and, later, a reflected peak of −0.33. What is the medium?
Show the solution
- Reflection coefficient.
- Refractive index.
- Permittivity. The simulator's slightly slow waves inside the dielectric blur the last digit.
Answer a dielectric with n ≈ 2.02 and ≈ 4.1.
Worked example
A pulse's peak passes probes 200 cells apart 400 steps apart, at S = 1. How fast is it?
Show the solution
- Speed. At S = 1 a wave in free space moves one cell per step, so 200 cells in 400 steps is 0.5c.
- The medium.
Answer it travels at 0.5c, so the medium has n = 2 and = 4.
Check
A pulse in vacuum enters a dielectric with = 4. What is the transmitted peak relative to the incident peak?
Check
A pulse's peak takes 200 steps to travel 100 cells at Courant number 0.5. What is its speed, as a fraction of c?
Check
Write step(E, H, eps, S=1.0): one Yee update of the NumPy arrays E and H in place (H first, then E, with E[0] and E[-1] left at zero), returning them.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write reflection(er): simulate a 1000-cell grid, vacuum up to cell 600 and = er from there, with a soft Gaussian source at cell 400 (centered at step 40, width 12) and a probe at cell 500, for 700 steps at S = 1. Return the reflected peak divided by the incident peak (the largest-magnitude values in the first 200 and the remaining steps, with their signs).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write wave_speed(er): fill an 1000-cell grid with = er, launch the same Gaussian at cell 200 (S = 1), record E at probes at cells 400 and 600 for 900 steps, and return the speed as a fraction of c from the steps between the two probes' peaks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a soft source launch pulses in both directions, and why does the measurement need a long grid?
A soft source adds to the electric field at one cell, which is like injecting a current sheet there; the fields it creates spread symmetrically, one pulse each way, because nothing in the update prefers a direction. The left-going pulse travels to the metal wall at cell 0, reflects, and comes back past the probe; the transmitted pulse reflects from the far wall too. If either arrived at the probe before the measurement window ended, it would be mistaken for part of the reflection. Making the grid long enough that those stray pulses arrive late, or adding absorbing boundaries, keeps the measurement clean.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.