Techniqueadvanced
Perfectly matched layer
Also called PML.
An absorbing layer at the domain boundary built by complex coordinate stretching, so outgoing waves enter it without reflection and decay.
A perfectly matched layer (PML) is an artificial absorbing layer around a computational domain for wave equations in open space. Inside the layer the coordinate normal to the boundary is continued into the complex plane: is replaced by , with in the interior and in the layer. A wave becomes in the layer, so it decays, and because the stretched equations agree with the original ones where , the interface has zero reflection for every angle and every frequency. This holds for the continuous equations only. On a grid the layer reflects a little, and the reflection is kept small by turning on gradually, often as with depth , layer depth and from 2 to 4. Berenger (1994) introduced the layer for Maxwell's equations and the Yee scheme. Chew and Weedon (1994) gave the interpretation as stretched coordinates, which carries the PML to acoustics, elasticity and other wave equations.
In plain words
A computer can only hold a finite piece of space. When a wave reaches the edge of that piece, a simple boundary sends it back, and the echo spoils the result. A perfectly matched layer is a thin border around the domain where waves are slowly damped. The damping is made in a special way: the border looks exactly like empty space to a wave that enters it, so the wave goes in without an echo, whatever its direction and frequency. Inside the border the wave dies out before it reaches the outer wall. On a computer the match is not quite perfect, so the damping must start softly and grow with depth.
When it applies
- Wave problems in an unbounded region, truncated to a finite domain: electromagnetics, acoustics, elastic waves, water waves, and the Schrödinger equation.
- Time-domain schemes such as the Yee scheme and frequency-domain solvers such as finite elements for the Helmholtz equation.
- Outgoing waves at all angles, where first-order absorbing conditions fail at grazing incidence.
- Problems where a few cells of extra domain at each side are cheap compared with the cost of an echo.
It does not apply to waves that do not propagate outward with a positive group velocity in the normal direction. Some anisotropic and elastic media have backward waves, and there the standard PML grows instead of decaying.
The template
For the one-dimensional wave equation in the first-order form , (units with ), the stretching gives and , with the same in both equations. The reference code adds this layer to the one-dimensional Yee scheme with at and at :
PML-STEP(E, H, σ, Δx, Δt)- 1
σ_max ← -(m + 1) c ln R / (2D) ▷ once, from the target reflection R - 2
σ(x) ← σ_max (d(x)/D)^m in the layer, 0 inside ▷ once, at the E and H points - 3
for each i do H_(i+1/2) ← ((1 - σΔt/2) H_(i+1/2) + (Δt/Δx)(E_(i+1) - E_i)) / (1 + σΔt/2) - 4
for each interior i do E_i ← ((1 - σΔt/2) E_i + (Δt/Δx)(H_(i+1/2) - H_(i-1/2))) / (1 + σΔt/2) - 5
E ← 0 at the two ends ▷ a wall behind the layer
Walk-through. L1 picks the peak conductivity from the reflection that the continuous layer would have. For a wave at angle to the normal crosses the layer, is reflected by the wall and comes back, so its amplitude is multiplied by
L2 grades the conductivity. L3 and L4 are the Yee updates with the damping term averaged between the old and the new time level, which keeps the scheme second order and stable for any . In the interior and they are the plain Yee scheme. L5 closes the domain with a wall, because any wave that reaches it has already lost the factor .
Why the continuous layer is perfect. Let . At the frequency the layer equations are the free equations in the variable , because . So the outgoing solution of the free equations solves the layer equations, and it equals the incident wave for , where . No reflected wave is needed to satisfy the interface conditions. The same argument works in several dimensions for each coordinate that is stretched, so the interface does not reflect at any angle. The factor gives the decay, and makes it independent of the frequency.
Classic examples
- Yee scheme: the original use. Berenger split each field component into two parts with separate conductivities; the convolutional PML (CPML) of Roden and Gedney (2000) does the same with one auxiliary variable per derivative and is the standard in FDTD codes.
- Wave equation and acoustics: the stretched form of the pressure and velocity equations, as in the reference code, and second-order forms with auxiliary fields for seismic modelling.
- Finite element method for the Helmholtz equation: in the frequency domain the PML is only a change of the coefficients, , with no extra unknowns.
- Discontinuous Galerkin method: DG solvers for Maxwell's equations and acoustics add the PML auxiliary equations as extra source terms in the layer elements.
- The Schrödinger equation with a Crank-Nicolson scheme: a complex stretching absorbs outgoing wave packets in quantum scattering and ionization problems.
One wave by hand. Take the design , a layer of cells with , and . L1 gives . At normal incidence , so one pass through the layer multiplies the amplitude by , and the way back by another . At , and the same layer gives only . At grazing incidence the layer gives no damping at all, which is why the domain should be large enough that the important waves meet the layer at moderate angles.
Reflection (measurement). A Gaussian pulse , , moving right in an interior of 200 cells with , enters a layer of cells designed for . The table shows the largest in the interior after the reflected part has come back, measured against the same scheme on a domain so long that no wave reaches its ends. With no layer, a wall gives 0.988.
| layer cells | (step) | |||
|---|---|---|---|---|
| 5 | 3.09e-1 | 3.77e-2 | 5.01e-4 | 2.95e-4 |
| 10 | 1.66e-1 | 8.84e-3 | 5.46e-5 | 2.26e-5 |
| 20 | 8.49e-2 | 2.18e-3 | 6.77e-6 | 1.41e-6 |
| 40 | 4.25e-2 | 5.38e-4 | 9.84e-7 | 1.03e-6 |
The design value is the same in every cell of the table, but the discrete layer reaches it only when it is thick and smooth. A step in reflects about , and the reflection falls only as . A linear profile gives . With or and 20 to 40 cells, the reflection is at the design level. With and , the designs and give the measured and , and the design gives , which is the floor of the discretization. A layer with loss only in (no matching loss in ) reflects 0.28 of the pulse, where the matched layer reflects .
Variations
- Split-field PML. Berenger's form: each field is split into the parts that are differentiated in and in , each with its own conductivity. It is not a strongly well-posed system, which can matter for some schemes.
- Uniaxial PML (UPML). An anisotropic artificial material with tensors and that has the same effect without splitting, and is a physical (though artificial) medium.
- Complex frequency shifted PML and CPML. The stretching with a small damps evanescent and low-frequency waves better and removes the late-time growth of the standard layer. The convolutional form implements it with a recursive update.
- Real stretching. A term in the stretching speeds up the decay of evanescent waves, which a pure imaginary stretching does not damp.
- Sponge layers and absorbing boundary conditions. A simple damping term without matching, or a local condition such as the first-order Engquist-Majda condition. They are cheaper but reflect much more, especially at grazing angles.
Pitfalls
- A sudden jump in . The continuous layer does not reflect, but the discrete one does, by an amount that grows with . Grade smoothly and use at least 8 to 10 cells.
- Too large . A small design with a thin layer makes change fast, and the discrete reflection then grows. The measured reflection, not the design value, is what counts.
- Unmatched loss. In electromagnetics the magnetic conductivity must satisfy . In other systems every equation that contains the stretched derivative needs the same stretching.
- Grazing and evanescent waves. The damping is proportional to , and evanescent waves are not damped by an imaginary stretching. Keep sources away from the layer, or add a real stretching and a frequency shift .
- Backward waves. In some anisotropic or elastic media the phase and group velocities point in opposite directions, and the standard PML becomes unstable. These media need modified layers.
- The layer in the wrong place. The PML must surround the domain on every side where waves leave. Corners need the stretching in both directions at once.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/perfectly-matched-layer/. The authors are listed in prov/D5V4KY.jsonl.
"""A graded perfectly matched layer for the one-dimensional Yee scheme.
Equations (units with eps = mu = c = 1): E_t + sigma E = H_x, H_t + sigma H = E_x.
With the same sigma in both equations the layer has the impedance of free
space, so in the continuous problem a wave enters it without reflection and
decays as exp(-integral of sigma). The grid has E at x_i = i dx, i = 0..n,
and H at x_{i+1/2}; E = 0 at both ends (a perfect conductor behind the layer).
"""
import math
def sigma_max_for(R, depth, m):
"""Peak conductivity that gives the reflection R of the continuous layer."""
# @line L1: sigma_max from the target reflection, the depth D and the grading m
return -(m + 1) * math.log(R) / (2 * depth)
def theoretical_reflection(sigma_max, depth, m, cos_theta=1.0):
"""R = exp(-2 cos(theta) integral_0^D sigma), for sigma = sigma_max (d / D)^m."""
return math.exp(-2 * cos_theta * sigma_max * depth / (m + 1))
def profiles(n, npml, dx, sigma_max, m):
"""sigma at the E points and at the H points, graded into the layers at both ends."""
D = npml * dx
def depth(x):
return max(npml * dx - x, x - (n - npml) * dx, 0.0)
# @line L2: sigma(d) = sigma_max (d / D)^m at depth d inside the layer
sE = [sigma_max * (depth(i * dx) / D) ** m if depth(i * dx) > 0 else 0.0
for i in range(n + 1)]
sH = [sigma_max * (depth((i + 0.5) * dx) / D) ** m if depth((i + 0.5) * dx) > 0 else 0.0
for i in range(n)]
return sE, sH
def pml_step(E, H, sE, sH, dx, dt):
n = len(H)
for i in range(n):
s = sH[i] * dt / 2
# @line L3: lossy H update (sigma averaged in time)
H[i] = ((1 - s) * H[i] + dt / dx * (E[i + 1] - E[i])) / (1 + s)
for i in range(1, n):
s = sE[i] * dt / 2
# @line L4: lossy E update
E[i] = ((1 - s) * E[i] + dt / dx * (H[i] - H[i - 1])) / (1 + s)
# @line L5: a conductor behind the layer
E[0] = E[n] = 0.0
def simulate(n, npml, dx, dt, steps, sigma_max, m, pulse):
"""Run a right-moving pulse E = f(x - t), H = -f(x - t) and return E at the end."""
sE, sH = profiles(n, npml, dx, sigma_max, m)
E = [pulse(i * dx) for i in range(n + 1)]
H = [-pulse((i + 0.5) * dx + dt / 2) for i in range(n)]
E[0] = E[n] = 0.0
for _ in range(steps):
pml_step(E, H, sE, sH, dx, dt)
return E
def reflection(npml, sigma_max, m, interior=200, dx=1.0, lam=0.5, width=6.0):
"""Largest reflected |E| in the interior, relative to a unit pulse.
The reference is the same scheme on a domain so long at both ends that
no wave reaches them, so numerical dispersion cancels in the difference.
"""
dt = lam * dx
x0 = (npml + interior / 2) * dx
travel = interior / 2 + 2 * npml + interior / 4 # out, through the layer, back
steps = int(travel / lam)
n = interior + 2 * npml
E = simulate(n, npml, dx, dt, steps, sigma_max, m,
lambda x: math.exp(-((x - x0) / width) ** 2))
L = int(steps * lam) + 4 * int(width) # extra cells at each end
E_ref = simulate(n + 2 * L, 0, dx, dt, steps, 0.0, 0,
lambda x: math.exp(-((x - x0 - L * dx) / width) ** 2))
lo, hi = npml, npml + interior
return max(abs(E[i] - E_ref[i + L]) for i in range(lo, hi + 1))
test_perfectly_matched_layer.py 70 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/perfectly-matched-layer/. The authors are listed in prov/D5V4KY.jsonl.
import math
from perfectly_matched_layer import (pml_step, profiles, reflection, sigma_max_for,
theoretical_reflection)
def test_sigma_max_and_theoretical_reflection_are_inverse():
for R in (1e-2, 1e-6):
for m in (0, 2, 3):
s = sigma_max_for(R, 12.0, m)
assert abs(theoretical_reflection(s, 12.0, m) - R) < 1e-12 * max(1, R)
s = sigma_max_for(1e-4, 10.0, 2)
assert theoretical_reflection(s, 10.0, 2, cos_theta=0.5) > 1e-4 # oblique waves see less
def test_profiles_are_zero_inside_and_graded_in_the_layer():
sE, sH = profiles(30, 5, 1.0, 2.0, 2)
assert all(v == 0.0 for v in sE[5:26]) and all(v == 0.0 for v in sH[5:25])
assert sE[0] == sE[30] == 2.0
assert all(sE[i] > sE[i + 1] for i in range(5))
assert sH == sH[::-1] and sE == sE[::-1]
def test_conductor_without_layer_reflects_everything():
assert reflection(0, 0.0, 0, interior=120) > 0.95
def test_layer_with_zero_conductivity_is_just_more_free_space():
# the pulse goes on into the layer and comes back from the wall behind it
assert reflection(10, 0.0, 0, interior=120) > 0.95
def test_graded_layer_is_far_better_than_a_step_in_conductivity():
step = reflection(20, sigma_max_for(1e-6, 20.0, 0), 0, interior=120)
graded = reflection(20, sigma_max_for(1e-6, 20.0, 2), 2, interior=120)
assert step > 0.05 and graded < 1e-4
def test_discrete_reflection_falls_as_the_layer_gets_thicker():
r = [reflection(N, sigma_max_for(1e-6, float(N), 2), 2, interior=120) for N in (5, 10, 20)]
assert r[0] > r[1] > r[2]
assert r[0] > 1e-6 # the discrete layer is not perfect
def test_a_weak_smooth_layer_reaches_its_design_reflection():
R = 1e-2
got = reflection(20, sigma_max_for(R, 20.0, 3), 3, interior=120)
assert 0.8 * R < got < 1.2 * R
def test_unmatched_layer_reflects():
# loss only in E breaks the impedance match: E_t + sigma E = H_x, H_t = E_x
n, npml, dx, dt = 200, 20, 1.0, 0.5
smax = sigma_max_for(1e-6, 20.0, 2)
x0, width = 100.0, 6.0
out = []
for matched in (True, False):
sE, sH = profiles(n, npml, dx, smax, 2)
if not matched:
sH = [0.0] * n
E = [math.exp(-((i - x0) / width) ** 2) for i in range(n + 1)]
H = [-math.exp(-((i + 0.5 + dt / 2 - x0) / width) ** 2) for i in range(n)]
E[0] = E[n] = 0.0
for _ in range(int(2 * (80 + 40 + 30))):
pml_step(E, H, sE, sH, dx, dt)
out.append(max(abs(v) for v in E[npml:n - npml]))
assert out[0] < 1e-3 and out[1] > 10 * out[0]
Exercises
- Insert , with into and , and show that it is a solution for . Show that decays as .
- Show that L3 and L4 are second-order accurate for the layer equations, and that with the discrete energy of the Yee scheme does not increase.
- For a layer with a constant that starts with a jump at , compute the reflection coefficient of the discrete scheme for a wave with , and show that it is of order .
References
- Jean-Pierre Berenger, A perfectly matched layer for the absorption of electromagnetic waves, Journal of Computational Physics 114(2) (1994) 185-200, the split-field layer for Maxwell's equations, the matching condition on the electric and magnetic conductivities, the proof of zero reflection at all angles and frequencies
- Allen Taflove and Susan C. Hagness, Computational Electrodynamics, The Finite-Difference Time-Domain Method, 3rd edition, Artech House, 2005, Chapter 7 (perfectly matched layer absorbing boundary conditions, polynomial grading, the reflection estimate, CPML)
- Steven G. Johnson, Notes on perfectly matched layers (PMLs), PML as complex coordinate stretching, the decay of propagating waves, why the discrete layer reflects and the need for gradual turn-on
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag D5V4KY Edit this page History Report an error Markdown