Algorithm Search⌘K

Modelintro

Gerstner waves

Also called trochoidal waves.

Model ocean waves by moving surface points on circles, which gives sharp crests and flat troughs, summed over several wave trains.

A Gerstner wave moves each point of the water surface on a circle, so that the points crowd together at the crests and spread out in the troughs. The surface that results is a trochoid: the crests are sharper and the troughs flatter than those of a sine wave of the same height. Gerstner found in 1802 that this motion is an exact solution of the equations of an ideal fluid in deep water, with a free surface at a constant pressure. In computer graphics, Fournier and Reeves (1986) and Peachey (1986) used trochoidal waves for the sea, and real-time water adds a few of them in different directions, as in Finch (2004). The waves come from a closed formula, so there is no simulation and no grid.

The machine

One wave, the exact model. The water fills z≤0z \le 0 at rest and is deep compared with the wavelength. Each water particle has a label (a,b)(a, b), its rest position, with b≤0b \le 0. At the time tt it is at

x=a−Aekbsin⁡θ,z=b+Aekbcos⁡θ,θ=ka−ωt+φ,x = a - A e^{kb}\sin\theta, \qquad z = b + A e^{kb}\cos\theta, \qquad \theta = ka - \omega t + \varphi,

and the surface is the curve of the particles with b=0b = 0. The parameters are:

  • the amplitude A≥0A \ge 0 in m,
  • the wave number k=2π/λ>0k = 2\pi/\lambda > 0 in rad/m, for the wavelength λ\lambda in m,
  • the angular frequency ω\omega in rad/s, fixed by the deep water dispersion relation ω2=gk\omega^2 = g k, with g=9.81g = 9.81 m/s²,
  • a phase φ\varphi in rad,
  • the steepness kAkA, with no loops for kA≤1kA \le 1; the exact model needs kA<1kA < 1.

The wave runs in the +x+x direction at the phase speed c=ω/k=(g/k)1/2c = \omega/k = (g/k)^{1/2}. The standing assumptions are deep water, an incompressible fluid with no viscosity, gravity as the only force, a constant pressure on the surface, no wind and no surface tension. The surface is a parametric curve, not a height field, so it can hold a sharp crest.

A sum of waves, the graphics model. On a 3D surface, the rest point p=(px,py)p = (p_x, p_y) moves to

(p−Q∑id^iAisin⁡θi, ∑iAicos⁡θi),θi=ki d^i⋅p−ωit+φi,\Big(p - Q\sum_i \hat d_i A_i \sin\theta_i,\ \sum_i A_i\cos\theta_i\Big), \qquad \theta_i = k_i\,\hat d_i\cdot p - \omega_i t + \varphi_i,

with a unit direction d^i\hat d_i, an amplitude AiA_i and a wave number kik_i for each wave, ωi2=gki\omega_i^2 = g k_i, and a choppiness Q∈[0,1]Q \in [0, 1] that scales the horizontal motion. For one wave and Q=1Q = 1, this is the exact model. A sum of exact solutions of these nonlinear equations is not shown to be a solution, so the sum is a model for images, not a solution of the flow.

What it counts

The model gives the position of the surface at each time: the height and the horizontal displacement of each rest point. It also gives the velocity of each particle, (Aekbωcos⁡θ, Aekbωsin⁡θ)(A e^{kb}\omega\cos\theta,\ A e^{kb}\omega\sin\theta) for one wave, and so the motion of a floating object that follows the water. The surface normal comes from the two tangents ∂/∂px\partial/\partial p_x and ∂/∂py\partial/\partial p_y of the formula.

For one wave of wavelength 10 m on deep water, k=0.628319k = 0.628319 rad/m, ω=2.482701\omega = 2.482701 rad/s, the period is 2.5307862.530786 s and the phase speed is 3.9513423.951342 m/s. The table gives the values that the model predicts for three amplitudes of this wave, computed by the code. The mean level is the mean surface height over xx.

AA (m) kAkA mean level (m) crest above the mean (m) trough below the mean (m) particle speed at the surface (m/s) vorticity at the surface (1/s) part of the wavelength above the mean
0.3 0.189 -0.028 0.328 0.272 0.745 0.183 0.470
0.8 0.503 -0.201 1.001 0.599 1.986 1.679 0.426
1.592 1.000 -0.796 2.387 0.796 3.951 no limit 0.391

The crest stands higher above the mean level than the trough falls below it, and it is narrower; at kA=1kA = 1 the crest is a cusp. Five meters below the surface, the orbits of this wave have only e−5k=4.3e^{-5k} = 4.3 percent of the radius at the surface.

Relation to other models

  • Linear (sine) waves. For small kAkA, the height of a Gerstner wave at a fixed xx is Acos⁡(kx−ωt)A\cos(kx - \omega t) plus terms of order kA2kA^2. So to first order it is the sine wave of linear theory, with the same dispersion relation.
  • Stokes waves. A wave that starts from water at rest in an ideal fluid has no vorticity, by the theorem of Kelvin. The Gerstner flow has vorticity, so it is not that wave; the periodic waves of the irrotational theory are the Stokes waves, which this page does not cover.
  • FFT ocean synthesis. The choppy displacement of FFT ocean synthesis moves each point by −λ k^-\lambda\,\hat k times a sine term for each wave, the horizontal motion of a sum of Gerstner waves with random amplitudes and phases. FFT synthesis uses thousands of waves from a spectrum; a Gerstner sum uses a few that the artist chooses.
  • Height field water. Height field water and the shallow water equations simulate the water and so react to objects and to the ground, but have no sharp crests. A Gerstner wave reacts to nothing.
  • Shallow water. Near a beach the deep water assumption fails. Fournier and Reeves, and Peachey, changed the waves with the depth of the water; with depth dd, the linear dispersion relation becomes ω2=gktanh⁡(kd)\omega^2 = gk\tanh(kd), and for long waves the speed tends to (gd)1/2(gd)^{1/2}, which is 3.132093.13209 m/s for d=1d = 1 m.
  • Fluid equations. The model is a special solution of the incompressible Euler equations, the Navier-Stokes equations with no viscosity.

Known results in the model

The proof page has the proofs, and the tests check each result with the code.

  • Exact solution (proved). With ω2=gk\omega^2 = gk and kA<1kA < 1, the motion satisfies the Euler equations in Lagrangian form, with a pressure that depends on bb only. So the pressure is constant on the surface b=0b = 0, as the free surface needs. With any other ω\omega, the equations fail.
  • Incompressible (proved). The Jacobian of the map from (a,b)(a, b) to (x,z)(x, z) is 1−k2A2e2kb1 - k^2A^2e^{2kb}, which does not change in time, so every part of the water keeps its area.
  • Circular orbits (proved). Each particle moves on a circle of radius AekbAe^{kb} about its label, once per period, so the radius falls by the factor e−ke^{-k} for each meter of depth.
  • Rotational flow (proved). The vorticity is 2ωs2/(1−s2)2\omega s^2/(1 - s^2) with s=kAekbs = kAe^{kb}. It is not 0, and it turns the other way from the particles on their orbits.
  • Shape (proved). The crest is at z=Az = A and the trough at z=−Az = -A, but the mean level of the surface over xx is −kA2/2-kA^2/2. The surface is stretched by the factor 1−kA1 - kA at the crest and 1+kA1 + kA at the trough, so the crest is sharp. At kA=1kA = 1 the crest is a cusp, and for kA>1kA > 1 the curve crosses itself and makes a loop. The part of the wavelength above the mean level is measured in the table.
  • Crest speed (proved). At the crest the surface particle moves forward at AωA\omega, which equals the phase speed ω/k\omega/k exactly when kA=1kA = 1.
  • No loops in a sum (proved). If Q∑ikiAi<1Q\sum_i k_iA_i < 1, the horizontal map p↦x(p)p \mapsto x(p) is one to one at every time, so the 3D surface does not fold over itself. Finch (2004) gives the same kind of condition: the sum of QikiAiQ_i k_i A_i at most 1, with one QiQ_i for each wave. The condition is sufficient, not necessary.
  • Dispersion (law). ω2=gk\omega^2 = gk is the dispersion law of small waves on deep water, and the Gerstner wave has it for every amplitude.
Proof: The Gerstner wave is an exact rotational solution, and a sum with small steepness has no loopsfull

We use the notation of the page. For one wave, write E=AekbE = Ae^{kb}, s=kEs = kE and θ=ka−ωt+φ\theta = ka - \omega t + \varphi, so that x=a−Esin⁡θx = a - E\sin\theta and z=b+Ecos⁡θz = b + E\cos\theta. The derivatives with respect to the labels are

xa=1−scos⁡θ,xb=−ssin⁡θ,za=−ssin⁡θ,zb=1+scos⁡θ,x_a = 1 - s\cos\theta, \quad x_b = -s\sin\theta, \quad z_a = -s\sin\theta, \quad z_b = 1 + s\cos\theta,

because ∂E/∂b=kE\partial E/\partial b = kE. The velocity is u=x˙=Eωcos⁡θu = \dot x = E\omega\cos\theta and w=z˙=Eωsin⁡θw = \dot z = E\omega\sin\theta, and the acceleration is x¨=Eω2sin⁡θ\ddot x = E\omega^2\sin\theta and z¨=−Eω2cos⁡θ\ddot z = -E\omega^2\cos\theta.

Area and orbits

Claim 1. The Jacobian is J=xazb−xbza=1−s2J = x_a z_b - x_b z_a = 1 - s^2. It does not depend on tt, so the flow keeps the area of every part of the water. For kA<1kA < 1 the map from the labels b≤0b \le 0 to the positions is one to one at every time.

Proof. J=(1−scos⁡θ)(1+scos⁡θ)−s2sin⁡2θ=1−s2J = (1 - s\cos\theta)(1 + s\cos\theta) - s^2\sin^2\theta = 1 - s^2. Area preservation is the statement that JJ is constant in time. For one to one, note that (x,z)=∇Ψ(x, z) = \nabla\Psi for Ψ(a,b)=12(a2+b2)+(A/k)ekbcos⁡θ\Psi(a, b) = \frac12(a^2 + b^2) + (A/k)e^{kb}\cos\theta. Its Hessian is the symmetric matrix with the entries xax_a, xb=zax_b = z_a and zbz_b, with trace 2 and determinant 1−s21 - s^2, so its eigenvalues are 1±s≥1−kA>01 \pm s \ge 1 - kA > 0 on b≤0b \le 0. The half plane b≤0b \le 0 is convex, so for two labels P≠P′P \ne P' in it, (∇Ψ(P)−∇Ψ(P′))⋅(P−P′)=∫01(P−P′)T∇2Ψ(P′+r(P−P′))(P−P′) dr≥(1−kA)∣P−P′∣2>0(\nabla\Psi(P) - \nabla\Psi(P'))\cdot(P - P') = \int_0^1 (P - P')^{\mathsf T}\nabla^2\Psi(P' + r(P - P'))(P - P')\, dr \ge (1 - kA)|P - P'|^2 > 0, and the two positions differ. □\square

Claim 2. Each particle moves on the circle of radius E=AekbE = Ae^{kb} about (a,b)(a, b), once per period 2π/ω2\pi/\omega.

Proof. (x−a)2+(z−b)2=E2(sin⁡2θ+cos⁡2θ)=E2(x - a)^2 + (z - b)^2 = E^2(\sin^2\theta + \cos^2\theta) = E^2, and θ\theta changes by 2π2\pi in the time 2π/ω2\pi/\omega. □\square

The exact solution

Claim 3. If ω2=gk\omega^2 = gk and s<1s < 1, the motion satisfies the Euler equations of an incompressible ideal fluid under gravity, with the pressure

pρ=p0ρ−gb+gkA22e2kb,\frac{p}{\rho} = \frac{p_0}{\rho} - g b + \frac{g k A^2}{2} e^{2kb},

which is constant on the surface b=0b = 0. If ω2≠gk\omega^2 \ne gk, no pressure that is constant on the surface fits the motion.

Proof. The Euler equations are x¨=−px/ρ\ddot x = -p_x/\rho and z¨=−pz/ρ−g\ddot z = -p_z/\rho - g, and incompressibility holds by Claim 1. By the chain rule pa=pxxa+pzzap_a = p_x x_a + p_z z_a and pb=pxxb+pzzbp_b = p_x x_b + p_z z_b, so in the labels the equations are

−paρ=x¨ xa+(z¨+g)za,−pbρ=x¨ xb+(z¨+g)zb.-\frac{p_a}{\rho} = \ddot x\, x_a + (\ddot z + g) z_a, \qquad -\frac{p_b}{\rho} = \ddot x\, x_b + (\ddot z + g) z_b.

The first right side is Eω2sin⁡θ(1−scos⁡θ)+(g−Eω2cos⁡θ)(−ssin⁡θ)=Esin⁡θ (ω2−gk)E\omega^2\sin\theta(1 - s\cos\theta) + (g - E\omega^2\cos\theta)(-s\sin\theta) = E\sin\theta\,(\omega^2 - gk), since the terms ±sEω2sin⁡θcos⁡θ\pm sE\omega^2\sin\theta\cos\theta cancel and s=kEs = kE. The second right side is −sEω2sin⁡2θ+(g−Eω2cos⁡θ)(1+scos⁡θ)=g−sEω2+Ecos⁡θ (gk−ω2)-sE\omega^2\sin^2\theta + (g - E\omega^2\cos\theta)(1 + s\cos\theta) = g - sE\omega^2 + E\cos\theta\,(gk - \omega^2). With ω2=gk\omega^2 = gk, the first is 0 and the second is g−gk2A2e2kbg - gk^2A^2e^{2kb}, and these are −∂/∂a-\partial/\partial a and −∂/∂b-\partial/\partial b of the pressure above divided by ρ\rho. With ω2≠gk\omega^2 \ne gk, the first right side is not 0 at b=0b = 0 for sin⁡θ≠0\sin\theta \ne 0, so pa≠0p_a \ne 0 along the surface and the pressure there is not constant. □\square

Vorticity

Claim 4. The vorticity is ∂w/∂x−∂u/∂z=2ωs2/(1−s2)\partial w/\partial x - \partial u/\partial z = 2\omega s^2/(1 - s^2), which is positive, while the particles turn clockwise.

Proof. Inverting the label map gives ∂a/∂x=zb/J\partial a/\partial x = z_b/J, ∂b/∂x=−za/J\partial b/\partial x = -z_a/J, ∂a/∂z=−xb/J\partial a/\partial z = -x_b/J and ∂b/∂z=xa/J\partial b/\partial z = x_a/J. With wa=sωcos⁡θw_a = s\omega\cos\theta, wb=sωsin⁡θw_b = s\omega\sin\theta, ua=−sωsin⁡θu_a = -s\omega\sin\theta and ub=sωcos⁡θu_b = s\omega\cos\theta: J ∂w/∂x=wazb−wbza=sωcos⁡θ+s2ωJ\,\partial w/\partial x = w_a z_b - w_b z_a = s\omega\cos\theta + s^2\omega and J ∂u/∂z=ubxa−uaxb=sωcos⁡θ−s2ωJ\,\partial u/\partial z = u_b x_a - u_a x_b = s\omega\cos\theta - s^2\omega. The difference is 2s2ω/J2s^2\omega/J. The position on the circle is E(−sin⁡θ,cos⁡θ)E(-\sin\theta, \cos\theta), at the angle θ+π/2\theta + \pi/2, which decreases in time, so the particles turn clockwise. □\square

Shape of the surface

Take b=0b = 0, t=0t = 0 and φ=0\varphi = 0, so θ=ka\theta = ka, s=kAs = kA, and the surface is x(a)=a−Asin⁡kax(a) = a - A\sin ka, z(a)=Acos⁡kaz(a) = A\cos ka. Other times and phases only shift it.

Claim 5. The crest is at z=Az = A and the trough at z=−Az = -A. The surface is stretched by xa=1−kAx_a = 1 - kA at the crest and 1+kA1 + kA at the trough. For kA≤1kA \le 1 the mean height over xx is −kA2/2-kA^2/2. At kA=1kA = 1 the crest is a cusp, and for kA>1kA > 1 the surface crosses itself.

Proof. The values of zz and xax_a at θ=0\theta = 0 and θ=π\theta = \pi are direct. For kA≤1kA \le 1, xx is nondecreasing and x(a+λ)=x(a)+λx(a + \lambda) = x(a) + \lambda, so the mean over one wavelength is 1λ∫0λz xa da=1λ∫0λ(Acos⁡ka−kA2cos⁡2ka) da=−kA2/2\frac1\lambda\int_0^\lambda z\, x_a\, da = \frac1\lambda\int_0^\lambda (A\cos ka - kA^2\cos^2 ka)\, da = -kA^2/2. At kA=1kA = 1, both xax_a and za=−kAsin⁡kaz_a = -kA\sin ka are 0 at a=0a = 0, and near it x−x(0)≈k2a3/6x - x(0) \approx k^2 a^3/6 while z−A≈−Ak2a2/2z - A \approx -Ak^2a^2/2, so ∣x−x(0)∣|x - x(0)| grows like ∣z−A∣3/2|z - A|^{3/2}: a cusp. For kA>1kA > 1, f(θ)=k(x−x(0))=θ−kAsin⁡θf(\theta) = k(x - x(0)) = \theta - kA\sin\theta is negative for small θ>0\theta > 0 and equals π\pi at θ=π\theta = \pi, so f(θ0)=0f(\theta_0) = 0 for some θ0∈(0,π)\theta_0 \in (0, \pi). Since ff is odd and zz is even in θ\theta, the points θ0\theta_0 and −θ0-\theta_0 have the same xx and the same zz, and they are different points of the curve. □\square

Claim 6. At the crest the surface particle moves forward at AωA\omega, and this equals the phase speed ω/k\omega/k exactly when kA=1kA = 1.

Proof. At θ=0\theta = 0, u=Aωu = A\omega and w=0w = 0. Aω=ω/kA\omega = \omega/k if and only if kA=1kA = 1. □\square

A sum of waves

Claim 7. Let μ=1−Q∑ikiAi>0\mu = 1 - Q\sum_i k_iA_i > 0. Then at every time the horizontal map p↦x(p)=p−Q∑id^iAisin⁡θip \mapsto x(p) = p - Q\sum_i \hat d_iA_i\sin\theta_i satisfies (x(p)−x(q))⋅(p−q)≥μ∣p−q∣2(x(p) - x(q))\cdot(p - q) \ge \mu|p - q|^2, so it is one to one, and its Jacobian matrix is symmetric with eigenvalues at least μ\mu.

Proof. x=∇Φx = \nabla\Phi for Φ(p)=12∣p∣2+Q∑i(Ai/ki)cos⁡θi\Phi(p) = \frac12|p|^2 + Q\sum_i (A_i/k_i)\cos\theta_i, because ∇cos⁡θi=−kid^isin⁡θi\nabla\cos\theta_i = -k_i\hat d_i\sin\theta_i. The Hessian is I−Q∑ikiAicos⁡θi d^id^iTI - Q\sum_i k_iA_i\cos\theta_i\, \hat d_i\hat d_i^{\mathsf T}, which is the Jacobian matrix of xx and is symmetric. For a unit vector vv, vTd^id^iTv∈[0,1]v^{\mathsf T}\hat d_i\hat d_i^{\mathsf T}v \in [0, 1], so vT(∇2Φ)v≥1−Q∑ikiAi=μv^{\mathsf T}(\nabla^2\Phi)v \ge 1 - Q\sum_i k_iA_i = \mu. Then (x(p)−x(q))⋅(p−q)=∫01(p−q)T∇2Φ(q+r(p−q))(p−q) dr≥μ∣p−q∣2(x(p) - x(q))\cdot(p - q) = \int_0^1 (p - q)^{\mathsf T}\nabla^2\Phi(q + r(p - q))(p - q)\, dr \ge \mu|p - q|^2, which is positive for p≠qp \ne q, so x(p)≠x(q)x(p) \ne x(q). ■\blacksquare

The test checks Claims 1 to 7 with the code, by finite differences for Claims 1, 3 and 4 and on random sums of waves for Claim 7.

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.

gerstner_waves.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gerstner-waves/. The authors are listed in prov/4CGSP7.jsonl.
"""Gerstner (trochoidal) waves on deep water.

One wave in 2D, in Lagrangian form. The water particle with the label (a, b),
b <= 0, is at

    x = a - A e^(k b) sin(theta),   z = b + A e^(k b) cos(theta),
    theta = k a - w t + phase,      w^2 = g k.

The surface is the line of particles with b = 0. The wave runs in the +x
direction, and each particle moves on a circle of radius A e^(k b).

The sum of several waves on a surface in 3D, as used in graphics, moves the
rest point p = (p_x, p_y) to

    p - chop * sum_i d_i A_i sin(theta_i),   height sum_i A_i cos(theta_i),
    theta_i = k_i d_i . p - w_i t + phase_i,

for unit directions d_i. A sum is not an exact solution of the flow
equations; it is a model of this file and of the graphics papers.
"""

import math

G = 9.81


def dispersion(k, g=G, depth=None):
    """The angular frequency w: (g k)^(1/2) in deep water, (g k tanh(k d))^(1/2) at the depth d."""
    if depth is None:
        return math.sqrt(g * k)
    return math.sqrt(g * k * math.tanh(k * depth))


def deep_water(wavelength, g=G):
    """(k, w, period, phase speed) of a deep water wave of the given wavelength (m)."""
    k = 2.0 * math.pi / wavelength
    w = dispersion(k, g)
    return k, w, 2.0 * math.pi / w, w / k


def phase_speed(k, g=G, depth=None):
    """w / k. In deep water (g / k)^(1/2); for k d small at the depth d it tends to (g d)^(1/2)."""
    return dispersion(k, g, depth) / k


def particle(a, b, t, amplitude, k, g=G, phase=0.0, w=None):
    """The position (x, z) of the particle with the label (a, b) at the time t. w defaults to (g k)^(1/2)."""
    if w is None:
        w = dispersion(k, g)
    r = amplitude * math.exp(k * b)
    theta = k * a - w * t + phase
    return a - r * math.sin(theta), b + r * math.cos(theta)


def particle_velocity(a, b, t, amplitude, k, g=G, phase=0.0):
    """The velocity (u, v) of the particle with the label (a, b): the time derivative of particle()."""
    w = dispersion(k, g)
    r = amplitude * math.exp(k * b)
    theta = k * a - w * t + phase
    return r * w * math.cos(theta), r * w * math.sin(theta)


def jacobian(a, b, amplitude, k):
    """det d(x, z) / d(a, b) = 1 - (k A e^(k b))^2. It does not depend on the time."""
    s = k * amplitude * math.exp(k * b)
    return 1.0 - s * s


def vorticity(b, amplitude, k, g=G):
    """The vorticity dv/dx - du/dz at the particle with the depth label b: 2 w s^2 / (1 - s^2), s = k A e^(k b).

    It is positive (counterclockwise), while the particles turn clockwise for a wave that runs in +x.
    """
    s = k * amplitude * math.exp(k * b)
    return 2.0 * dispersion(k, g) * s * s / (1.0 - s * s)


def surface_point(px, py, t, waves, chop=1.0, g=G):
    """The surface point (x, y, z) of the rest point (px, py) for a sum of waves.

    waves is a list of (amplitude, wavelength, (dx, dy), phase) with a unit direction (dx, dy).
    """
    x, y, z = px, py, 0.0
    for amp, wavelength, (dx, dy), phase in waves:
        k = 2.0 * math.pi / wavelength
        theta = k * (dx * px + dy * py) - dispersion(k, g) * t + phase
        s, c = math.sin(theta), math.cos(theta)
        x -= chop * dx * amp * s
        y -= chop * dy * amp * s
        z += amp * c
    return x, y, z


def horizontal_jacobian(px, py, t, waves, chop=1.0, g=G):
    """The 2 by 2 matrix d(x, y) / d(px, py) of surface_point, as ((j11, j12), (j21, j22))."""
    j11, j12, j22 = 1.0, 0.0, 1.0
    for amp, wavelength, (dx, dy), phase in waves:
        k = 2.0 * math.pi / wavelength
        theta = k * (dx * px + dy * py) - dispersion(k, g) * t + phase
        f = chop * k * amp * math.cos(theta)
        j11 -= f * dx * dx
        j12 -= f * dx * dy
        j22 -= f * dy * dy
    return (j11, j12), (j12, j22)


def steepness(waves, chop=1.0):
    """chop * sum_i k_i A_i. If it is below 1, the surface has no loops (see the proof page)."""
    return chop * sum(2.0 * math.pi / wl * amp for amp, wl, _, _ in waves)


def mean_level(amplitude, k, samples=4096):
    """The mean surface height over x of one wave (b = 0, t = 0), by a sum over equally spaced labels a.

    Each label carries the length dx/da da of the surface, so the mean is
    sum z dx/da / sum dx/da. The proof page shows it is -k A^2 / 2 for k A < 1.
    """
    num = 0.0
    den = 0.0
    for i in range(samples):
        theta = 2.0 * math.pi * i / samples
        dxda = 1.0 - k * amplitude * math.cos(theta)
        num += amplitude * math.cos(theta) * dxda
        den += dxda
    return num / den
test_gerstner_waves.py 166 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/gerstner-waves/. The authors are listed in prov/4CGSP7.jsonl.
import math
import random
import unittest

import gerstner_waves as gw


def euler_residuals(a, b, t, amp, k, w, g=gw.G, h=1e-4):
    """The two Lagrangian momentum equations, by central differences.

    Returns (x_tt x_a + (z_tt + g) z_a, x_tt x_b + (z_tt + g) z_b), which equal -p_a / rho and -p_b / rho.
    """
    def pos(aa, bb, tt):
        return gw.particle(aa, bb, tt, amp, k, g, w=w)

    xp, zp = pos(a, b, t + h)
    x0, z0 = pos(a, b, t)
    xm, zm = pos(a, b, t - h)
    xtt, ztt = (xp - 2 * x0 + xm) / h ** 2, (zp - 2 * z0 + zm) / h ** 2
    xa = (pos(a + h, b, t)[0] - pos(a - h, b, t)[0]) / (2 * h)
    za = (pos(a + h, b, t)[1] - pos(a - h, b, t)[1]) / (2 * h)
    xb = (pos(a, b + h, t)[0] - pos(a, b - h, t)[0]) / (2 * h)
    zb = (pos(a, b + h, t)[1] - pos(a, b - h, t)[1]) / (2 * h)
    return xtt * xa + (ztt + g) * za, xtt * xb + (ztt + g) * zb


class TestDispersion(unittest.TestCase):
    def test_deep_water_check_values(self):
        k, w, period, speed = gw.deep_water(10.0, 9.81)
        self.assertAlmostEqual(k, 0.628319, places=6)
        self.assertAlmostEqual(w, 2.482701, places=6)
        self.assertAlmostEqual(period, 2.530786, places=6)
        self.assertAlmostEqual(speed, 3.951342, places=6)
        self.assertAlmostEqual(gw.phase_speed(k), speed, places=12)

    def test_shallow_limit(self):
        k = 2 * math.pi / 2000.0
        self.assertAlmostEqual(gw.phase_speed(k, depth=1.0), 3.13209, places=4)
        self.assertAlmostEqual(math.sqrt(9.81 * 1.0), 3.13209, places=5)
        self.assertLess(gw.phase_speed(0.628319, depth=1.0), gw.phase_speed(0.628319))


class TestOneWave(unittest.TestCase):
    def test_circular_orbits(self):
        rng = random.Random(1)
        k, amp = 0.628319, 0.6
        for _ in range(50):
            a, b, t = rng.uniform(-20, 20), rng.uniform(-5, 0), rng.uniform(0, 10)
            x, z = gw.particle(a, b, t, amp, k)
            self.assertAlmostEqual(math.hypot(x - a, z - b), amp * math.exp(k * b), places=12)
            u, v = gw.particle_velocity(a, b, t, amp, k)
            self.assertAlmostEqual(math.hypot(u, v), amp * math.exp(k * b) * gw.dispersion(k), places=12)

    def test_crest_moves_at_phase_speed(self):
        k, amp = 0.628319, 0.5
        _, _, _, speed = gw.deep_water(2 * math.pi / k)
        for t in (0.0, 1.0, 3.7):
            crest = gw.dispersion(k) * t / k
            x, z = gw.particle(crest, 0.0, t, amp, k)
            self.assertAlmostEqual(x, speed * t, places=9)
            self.assertAlmostEqual(z, amp, places=12)

    def test_exact_euler_solution(self):
        rng = random.Random(2)
        k, amp = 0.5, 1.2
        w = gw.dispersion(k)
        for _ in range(20):
            a, b, t = rng.uniform(-10, 10), rng.uniform(-3, 0), rng.uniform(0, 5)
            ra, rb = euler_residuals(a, b, t, amp, k, w)
            self.assertLess(abs(ra), 1e-5)
            s = k * amp * math.exp(k * b)
            self.assertAlmostEqual(rb, gw.G * (1 - s * s), places=4)
        ra, _ = euler_residuals(1.0, 0.0, 0.3, amp, k, 1.2 * w)
        self.assertGreater(abs(ra), 0.1)

    def test_volume_and_vorticity(self):
        rng = random.Random(3)
        k, amp, h = 0.7, 1.0, 1e-5
        for _ in range(20):
            a, b, t = rng.uniform(-5, 5), rng.uniform(-2, 0), rng.uniform(0, 5)
            p = lambda aa, bb: gw.particle(aa, bb, t, amp, k)
            xa = [(p(a + h, b)[i] - p(a - h, b)[i]) / (2 * h) for i in (0, 1)]
            xb = [(p(a, b + h)[i] - p(a, b - h)[i]) / (2 * h) for i in (0, 1)]
            jac = xa[0] * xb[1] - xb[0] * xa[1]
            self.assertAlmostEqual(jac, gw.jacobian(a, b, amp, k), places=7)
            vel = lambda aa, bb: gw.particle_velocity(aa, bb, t, amp, k)
            ua = [(vel(a + h, b)[i] - vel(a - h, b)[i]) / (2 * h) for i in (0, 1)]
            ub = [(vel(a, b + h)[i] - vel(a, b - h)[i]) / (2 * h) for i in (0, 1)]
            dv_dx = (ua[1] * xb[1] - ub[1] * xa[1]) / jac
            du_dz = (ub[0] * xa[0] - ua[0] * xb[0]) / jac
            self.assertAlmostEqual(dv_dx - du_dz, gw.vorticity(b, amp, k), places=5)
        self.assertGreater(gw.vorticity(0.0, amp, k), 0.0)

    def test_crest_shape_and_loops(self):
        k = 1.0
        n = 2000

        def surface_x(amp):
            return [gw.particle(2 * math.pi * i / n, 0.0, 0.0, amp, k)[0] for i in range(-n // 2, n // 2)]

        for ka, loops in ((0.5, False), (0.99, False), (1.2, True)):
            xs = surface_x(ka / k)
            self.assertEqual(any(x2 <= x1 for x1, x2 in zip(xs, xs[1:])), loops)
        self.assertAlmostEqual(gw.jacobian(0.0, 0.0, 1.0, 1.0), 0.0, places=15)

    def test_mean_level(self):
        for k, amp in ((0.628319, 0.5), (1.0, 0.8), (0.2, 3.0)):
            self.assertAlmostEqual(gw.mean_level(amp, k), -k * amp * amp / 2, places=12)


class TestSum(unittest.TestCase):
    def waves(self, rng, count, total):
        out = []
        weights = [rng.uniform(0.2, 1.0) for _ in range(count)]
        for wt in weights:
            wl = rng.uniform(2.0, 40.0)
            ang = rng.uniform(0, 2 * math.pi)
            k = 2 * math.pi / wl
            out.append((total * wt / sum(weights) / k, wl, (math.cos(ang), math.sin(ang)), rng.uniform(0, 6)))
        return out

    def test_jacobian_matches_differences(self):
        rng = random.Random(4)
        waves = self.waves(rng, 5, 0.8)
        h = 1e-5
        for _ in range(10):
            px, py, t = rng.uniform(-30, 30), rng.uniform(-30, 30), rng.uniform(0, 9)
            (j11, j12), (j21, j22) = gw.horizontal_jacobian(px, py, t, waves)
            f = lambda qx, qy: gw.surface_point(qx, qy, t, waves)
            self.assertAlmostEqual(j11, (f(px + h, py)[0] - f(px - h, py)[0]) / (2 * h), places=6)
            self.assertAlmostEqual(j21, (f(px + h, py)[1] - f(px - h, py)[1]) / (2 * h), places=6)
            self.assertAlmostEqual(j12, (f(px, py + h)[0] - f(px, py - h)[0]) / (2 * h), places=6)

    def test_no_loops_below_steepness_one(self):
        rng = random.Random(5)
        for trial in range(5):
            waves = self.waves(rng, 6, 0.9)
            s = gw.steepness(waves)
            self.assertAlmostEqual(s, 0.9, places=12)
            for _ in range(200):
                p = (rng.uniform(-50, 50), rng.uniform(-50, 50))
                q = (rng.uniform(-50, 50), rng.uniform(-50, 50))
                t = rng.uniform(0, 20)
                fp = gw.surface_point(p[0], p[1], t, waves)
                fq = gw.surface_point(q[0], q[1], t, waves)
                dot = (fp[0] - fq[0]) * (p[0] - q[0]) + (fp[1] - fq[1]) * (p[1] - q[1])
                d2 = (p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2
                self.assertGreaterEqual(dot, (1 - s) * d2 - 1e-9)
                (j11, j12), (_, j22) = gw.horizontal_jacobian(p[0], p[1], t, waves)
                self.assertGreater(j11 * j22 - j12 * j12, 0.0)

    def test_one_wave_sum_equals_particle(self):
        k, amp = 0.628319, 0.4
        waves = [(amp, 2 * math.pi / k, (1.0, 0.0), 0.3)]
        for px in (-3.0, 0.0, 2.5):
            x, y, z = gw.surface_point(px, 7.0, 1.1, waves)
            xe, ze = gw.particle(px, 0.0, 1.1, amp, k, phase=0.3)
            self.assertAlmostEqual(x, xe, places=12)
            self.assertAlmostEqual(z, ze, places=12)
            self.assertEqual(y, 7.0)


if __name__ == "__main__":
    unittest.main()

References

  1. Alain Fournier and William T. Reeves, A simple model of ocean waves, Proceedings of SIGGRAPH 86, 75-84, 1986, the whole paper (the Gerstner model of the surface and the orbits of the particles)
  2. Darwyn R. Peachey, Modeling waves and surf, Proceedings of SIGGRAPH 86, 65-74, 1986, the whole paper (wave profiles, the depth of the water and spray)
  3. Mark Finch, Effective water simulation from physical models, Chapter 1 (the sum of Gerstner waves, the steepness parameter Q and the condition against loops at the crests)
  4. Simulating Ocean Water, the notes as a whole (Gerstner waves and the dispersion relation, general locator)

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 proofH2 full proof
  • correctness: no review record reviews/4CGSP7/proof-correctness.yaml
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)
WritingW4 complete
  • a review of the text: no review record reviews/4CGSP7/text.yaml