---
title: "Ocean and nonlinear lab: rays, modes, distortion and shocks"
heading: A ray traced through a sound-speed gradient layer by layer against its exact circular path, normal-mode cutoffs, group velocities and arrival spreads in shallow water, the harmonics of a distorting plane wave from Fubini's solution checked against the waveform itself, and shock jumps with blast scaling
eyebrow: S16 · Lesson 49 · Ocean and nonlinear lab
xp: 60
---

::: hook
Lessons 46 to 48 described sound in the ocean and sound too strong to stay linear, with formulas built on idealizations: constant gradients, ideal waveguides, plane waves short of shock formation. In this lab you'll compute each the way a modeling code would: trace a ray through thin layers, list a waveguide's modes and their arrivals, solve a distorting waveform directly and take its Fourier series, and evaluate shock jumps and blast scaling, checking every one against its closed form.
:::

::: play
- A plane wave steepens as it travels; σ is the distance traveled as a fraction of the distance at which a shock first forms.
- The solid curve is the fundamental's amplitude and the dashed curve the second harmonic's, from Fubini's solution, $B_n = 2J_n(n\sigma)/(n\sigma)$, relative to the original amplitude.
- Energy drains from the fundamental into harmonics as the wave steepens; by σ = 1 the second harmonic is about a third of the original amplitude.
:::

```sim plot
view: graph
x: s = 0.01..1
y: B = 0..1
plot: 2*J1(s)/s | label: fundamental
plot: J2(2*s)/s | dashed | label: second harmonic
```

::: predict
## A plane wave has traveled 80% of its shock-formation distance. How large is its second harmonic, relative to the original amplitude?

- [ ] About 3%
- [ ] About 10%
- [x] About 32%
- [ ] About 80%

::: reveal
Fubini's solution gives $B_2 = J_2(2\sigma)/\sigma$ = $J_2(1.6)/0.8$ = 0.32 at σ = 0.8, while the fundamental has fallen only to 0.92. Task 3 confirms it by solving for the distorted waveform point by point and taking its Fourier coefficients.
:::
:::

::: nameit
## What the lab computes

- **Rays**: in horizontal layers, Snell's law keeps $\cos\theta/c$ constant along a ray. Stepping down through layers of thickness Δz where c = c₀ + gz, the ray advances $\Delta z\cot\theta$ horizontally until $\cos\theta$ would exceed 1, where it turns. For a constant gradient the exact path is a circle of radius $R = c_0/(g\cos\theta_0)$, turning at depth $R(1 - \cos\theta_0)$ after a horizontal distance $R\sin\theta_0$.
- **Modes**: in water of depth D with a pressure-release surface and rigid bottom, mode n cuts off at $f_n = (2n - 1)c/4D$ and travels with group velocity $c\sqrt{1 - (f_n/f)^2}$; the arrival spread over a range is the difference between the slowest and fastest propagating modes' travel times.
- **Distortion**: before shock formation, a wave that started as sin φ is $u = \sin(\varphi + \sigma u)$ at relative distance σ, solved for u at each φ by Newton's method; its Fourier sine coefficients are Fubini's $B_n = 2J_n(n\sigma)/(n\sigma)$, with $J_n(x) = \frac1\pi\int_0^\pi\cos(n\tau - x\sin\tau)\,d\tau$.
- **Shocks**: across a shock of Mach number M in a gas of ratio γ, the pressure ratio $1 + \frac{2\gamma}{\gamma + 1}(M^2 - 1)$ and density ratio $\frac{(\gamma + 1)M^2}{(\gamma - 1)M^2 + 2}$; blasts scale as the cube root of charge mass, so a W-fold heavier charge gives the same overpressure at $W^{1/3}$ times the range.
:::

::: check
kind: python

## Write `trace_ray(c0, g, theta, dz=0.01)` returning (turning depth, horizontal distance to the turning point), by stepping through layers.

```python starter
import math

def trace_ray(c0, g, theta, dz=0.01):
    R = c0 / g
    return R * (1 - math.cos(theta)), R * math.sin(theta)
```

```python solution
import math

def trace_ray(c0, g, theta, dz=0.01):
    xi = math.cos(theta) / c0
    z = x = 0.0
    while True:
        s = xi * (c0 + g * (z + dz / 2))
        if s >= 1:
            break
        x += dz * s / math.sqrt(1 - s * s)
        z += dz
    return z, x
```

```python tests
import math
c0, g, th = 1500.0, 0.016, math.radians(2)
z, x = trace_ray(c0, g, th)
R = c0 / (g * math.cos(th))
assert abs(z - R * (1 - math.cos(th))) < 0.02, "It turns 57.1 m down, as the circle predicts."
assert abs(x / (R * math.sin(th)) - 1) < 0.02, "About 3.27 km out, within the layer discretization."
```

::: right
Right. Layer-by-layer tracing works for any profile; the circle is only the special case of a constant gradient.
:::
:::

::: check
kind: python

## Write `mode_arrivals(D, f, R, c=1500.0)` returning a list of (cutoff, group velocity, travel time) for every propagating mode, and the arrival spread.

```python starter
def mode_arrivals(D, f, R, c=1500.0):
    return [(0.0, c, R / c)], 0.0
```

```python solution
import math

def mode_arrivals(D, f, R, c=1500.0):
    out = []
    n = 1
    while True:
        fn = (2 * n - 1) * c / (4 * D)
        if fn >= f:
            break
        vg = c * math.sqrt(1 - (fn / f) ** 2)
        out.append((fn, vg, R / vg))
        n += 1
    times = [t for _, _, t in out]
    return out, max(times) - min(times)
```

```python tests
modes, spread = mode_arrivals(100, 100, 20000)
assert len(modes) == 13, "Thirteen modes propagate at 100 Hz in 100 m of water."
assert abs(modes[2][1] - 1473.40) < 0.01, "Mode 3 travels at 1473.4 m/s."
assert abs(modes[2][2] - modes[0][2] - 0.2314) < 1e-3, "It arrives 0.23 s after mode 1."
assert spread > 2, "The highest modes, near cutoff, arrive seconds later."
```

::: right
Right. Near cutoff a mode's group velocity drops toward zero, so the last modes straggle far behind.
:::
:::

::: check
kind: python

## Write `bessel(n, x)` by the integral with 2000 points, `fubini(sigma, n)`, and `waveform_harmonic(sigma, n, M=2048)`: the nth sine coefficient of the solved waveform.

```python starter
import math

def bessel(n, x):
    return 0.0

def fubini(sigma, n):
    return 1.0 if n == 1 else 0.0

def waveform_harmonic(sigma, n, M=2048):
    return 1.0 if n == 1 else 0.0
```

```python solution
import math

def bessel(n, x):
    N = 2000
    return sum(math.cos(n * (k + 0.5) * math.pi / N - x * math.sin((k + 0.5) * math.pi / N)) for k in range(N)) / N

def fubini(sigma, n):
    return 2 * bessel(n, n * sigma) / (n * sigma)

def waveform_harmonic(sigma, n, M=2048):
    total = 0.0
    for i in range(M):
        ph = 2 * math.pi * i / M
        u = math.sin(ph)
        for _ in range(100):
            u -= (u - math.sin(ph + sigma * u)) / (1 - sigma * math.cos(ph + sigma * u))
        total += u * math.sin(2 * math.pi * n * i / M)
    return 2 * total / M
```

```python tests
for s in (0.3, 0.8):
    for n in (1, 2, 3):
        assert abs(fubini(s, n) - waveform_harmonic(s, n)) < 1e-6, "Fubini's coefficients match the solved waveform."
assert abs(fubini(0.8, 2) - 0.3212) < 1e-4 and abs(fubini(0.8, 1) - 0.9221) < 1e-4, "At σ = 0.8: fundamental 0.92, second harmonic 0.32."
```

::: right
Right. Two independent routes, a Bessel series and a direct Fourier transform of the solved waveform, agree to six decimals.
:::
:::

::: check
kind: python

## Write `shock(M, g=1.4)` returning (pressure ratio, density ratio, temperature ratio) and `blast_range(R1, W1, W2)`: the range at which a charge of mass W2 matches the overpressure W1 gives at R1.

```python starter
def shock(M, g=1.4):
    return M * M, M, M

def blast_range(R1, W1, W2):
    return R1 * W2 / W1
```

```python solution
def shock(M, g=1.4):
    p = 1 + 2 * g / (g + 1) * (M * M - 1)
    rho = (g + 1) * M * M / ((g - 1) * M * M + 2)
    return p, rho, p / rho

def blast_range(R1, W1, W2):
    return R1 * (W2 / W1) ** (1 / 3)
```

```python tests
p, r, t = shock(2.0)
assert abs(p - 4.5) < 1e-12 and abs(r - 8 / 3) < 1e-12 and abs(t - 1.6875) < 1e-12, "Mach 2: pressure 4.5, density 2.67, temperature 1.69."
assert abs(shock(100.0)[1] - 6) < 0.01, "However strong the shock, the density ratio stays below (γ + 1)/(γ − 1) = 6."
assert abs(blast_range(10, 1, 1000) - 100) < 1e-9, "A tonne gives the same overpressure at 100 m as a kilogram at 10 m."
```

::: right
Right. Cube-root scaling follows from the energy filling a volume: a thousand times the energy reaches the same overpressure at ten times the radius.
:::
:::

::: explain
## Why does a strong sound wave steepen into a shock as it travels?

::: model
In a sound wave the local speed of propagation isn't quite constant: where the pressure is high, the air is compressed and warmed and moves forward with the wave, so the crest travels a little faster than c, while the trough travels a little slower. Over distance, the crest gains on the trough ahead of it and the waveform's front grows steeper, transferring energy from the fundamental into harmonics. When the front becomes vertical, the distance the linear theory can't pass, a shock forms, after which dissipation in the thin shock front limits further steepening and the wave loses energy as it goes.
:::
:::
