Algorithm Search⌘K

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: ∂x\partial_x is replaced by 11+iσ(x)/ω∂x\frac{1}{1 + i\sigma(x)/\omega}\partial_x, with σ=0\sigma = 0 in the interior and σ>0\sigma > 0 in the layer. A wave ei(kx−ωt)e^{i(kx - \omega t)} becomes ei(kx−ωt)e−(k/ω)∫σ dxe^{i(kx - \omega t)}e^{-(k/\omega)\int\sigma\,dx} in the layer, so it decays, and because the stretched equations agree with the original ones where σ=0\sigma = 0, 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 σ\sigma on gradually, often as σmax⁡(d/D)m\sigma_{\max}(d/D)^m with depth dd, layer depth DD and mm 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 Et=HxE_t = H_x, Ht=ExH_t = E_x (units with c=1c = 1), the stretching gives Et+σE=HxE_t + \sigma E = H_x and Ht+σH=ExH_t + \sigma H = E_x, with the same σ\sigma in both equations. The reference code adds this layer to the one-dimensional Yee scheme with EE at xix_i and HH at xi+1/2x_{i+1/2}:

  1. PML-STEP(E, H, σ, Δx, Δt)
  2. 1σ_max ← -(m + 1) c ln R / (2D) ▷ once, from the target reflection R
  3. 2σ(x) ← σ_max (d(x)/D)^m in the layer, 0 inside ▷ once, at the E and H points
  4. 3for each i do H_(i+1/2) ← ((1 - σΔt/2) H_(i+1/2) + (Δt/Δx)(E_(i+1) - E_i)) / (1 + σΔt/2)
  5. 4for each interior i do E_i ← ((1 - σΔt/2) E_i + (Δt/Δx)(H_(i+1/2) - H_(i-1/2))) / (1 + σΔt/2)
  6. 5E ← 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 σ=σmax⁡(d/D)m\sigma = \sigma_{\max}(d/D)^m a wave at angle θ\theta to the normal crosses the layer, is reflected by the wall and comes back, so its amplitude is multiplied by

R(θ)=exp⁡(−2cos⁡θc∫0Dσ dd)=exp⁡(−2cos⁡θ σmax⁡Dc (m+1)).R(\theta) = \exp\Big(-\frac{2\cos\theta}{c}\int_0^D\sigma\,dd\Big) = \exp\Big(-\frac{2\cos\theta\,\sigma_{\max}D}{c\,(m + 1)}\Big).

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 σ≥0\sigma \ge 0. In the interior σ=0\sigma = 0 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 R\sqrt{R}.

Why the continuous layer is perfect. Let x~=x+iω∫0xσ\tilde x = x + \frac{i}{\omega}\int_0^x\sigma. At the frequency ω\omega the layer equations are the free equations in the variable x~\tilde x, because ∂x~=11+iσ/ω∂x\partial_{\tilde x} = \frac{1}{1 + i\sigma/\omega}\partial_x. So the outgoing solution ei(kx~−ωt)e^{i(k\tilde x - \omega t)} of the free equations solves the layer equations, and it equals the incident wave for x≤0x \le 0, where x~=x\tilde x = x. 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 ∣eikx~∣=e−(kcos⁡θ/ω)∫σ|e^{ik\tilde x}| = e^{-(k\cos\theta/\omega)\int\sigma} gives the decay, and k/ω=1/ck/\omega = 1/c 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, ∂x→∂x/(1+iσ/ω)\partial_x \to \partial_x/(1 + i\sigma/\omega), 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 R=10−6R = 10^{-6}, a layer of D=20D = 20 cells with Δx=1\Delta x = 1, c=1c = 1 and m=2m = 2. L1 gives σmax⁡=3ln⁡(106)/40=1.036\sigma_{\max} = 3\ln(10^6)/40 = 1.036. At normal incidence ∫0Dσ=σmax⁡D/3=6.91\int_0^D\sigma = \sigma_{\max}D/3 = 6.91, so one pass through the layer multiplies the amplitude by e−6.91=10−3e^{-6.91} = 10^{-3}, and the way back by another 10−310^{-3}. At θ=60°\theta = 60°, cos⁡θ=12\cos\theta = \tfrac12 and the same layer gives only R=10−3R = 10^{-3}. 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 E=e−((x−x0)/6)2E = e^{-((x - x_0)/6)^2}, H=−EH = -E, moving right in an interior of 200 cells with Δt=Δx/2\Delta t = \Delta x/2, enters a layer of NN cells designed for R=10−6R = 10^{-6}. The table shows the largest ∣E∣|E| 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 NN m=0m = 0 (step) m=1m = 1 m=2m = 2 m=3m = 3
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 10−610^{-6} 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 σ\sigma reflects about σmax⁡Δx/4\sigma_{\max}\Delta x/4, and the reflection falls only as 1/N1/N. A linear profile gives 1/N21/N^2. With m=2m = 2 or 33 and 20 to 40 cells, the reflection is at the design level. With N=20N = 20 and m=3m = 3, the designs R=10−2R = 10^{-2} and 10−410^{-4} give the measured 9.8⋅10−39.8\cdot 10^{-3} and 1.1⋅10−41.1\cdot 10^{-4}, and the design 10−810^{-8} gives 1.9⋅10−61.9\cdot 10^{-6}, which is the floor of the discretization. A layer with loss only in EE (no matching loss in HH) reflects 0.28 of the pulse, where the matched layer reflects 6.7⋅10−66.7\cdot 10^{-6}.

Variations

  • Split-field PML. Berenger's form: each field is split into the parts that are differentiated in xx and in yy, 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 ε\varepsilon and μ\mu that has the same effect without splitting, and is a physical (though artificial) medium.
  • Complex frequency shifted PML and CPML. The stretching 1+σ/(α−iω)1 + \sigma/(\alpha - i\omega) with a small α>0\alpha > 0 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 κ(x)>1\kappa(x) > 1 in the stretching κ+iσ/ω\kappa + i\sigma/\omega 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 σ\sigma. The continuous layer does not reflect, but the discrete one does, by an amount that grows with σΔx\sigma\Delta x. Grade σ\sigma smoothly and use at least 8 to 10 cells.
  • Too large σmax⁡\sigma_{\max}. A small design RR with a thin layer makes σ\sigma 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 σ∗/μ=σ/ε\sigma^*/\mu = \sigma/\varepsilon. In other systems every equation that contains the stretched derivative needs the same stretching.
  • Grazing and evanescent waves. The damping is proportional to cos⁡θ\cos\theta, and evanescent waves are not damped by an imaginary stretching. Keep sources away from the layer, or add a real stretching κ\kappa and a frequency shift α\alpha.
  • 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.

perfectly_matched_layer.pyI0RawGitHub
# 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

  1. Insert E=ei(kx~−ωt)E = e^{i(k\tilde x - \omega t)}, H=−EH = -E with x~=x+iω∫0xσ\tilde x = x + \frac i\omega\int_0^x\sigma into Et+σE=HxE_t + \sigma E = H_x and Ht+σH=ExH_t + \sigma H = E_x, and show that it is a solution for c=1c = 1. Show that ∣E∣|E| decays as e−∫0xσe^{-\int_0^x\sigma}.
  2. Show that L3 and L4 are second-order accurate for the layer equations, and that with σ≥0\sigma\ge 0 the discrete energy of the Yee scheme does not increase.
  3. For a layer with a constant σ\sigma that starts with a jump at x=0x = 0, compute the reflection coefficient of the discrete scheme for a wave with kΔx→0k\Delta x \to 0, and show that it is of order σΔx\sigma\Delta x.

References

  1. 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
  2. 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)
  3. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises