Skip to content

cvc::ocean: the sea a floating body feels, and physics floats bodies on it (O7a) - #1142

Merged
transfix merged 2 commits into
masterfrom
feat/ocean-sea-surface
Nov 11, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/ocean-sea-surface

Conversation

@transfix

Copy link
Copy Markdown
Owner

O7a in docs/roadmap/OCEAN-AND-WEATHER-ROADMAP.md (B2–B4): the sea a floating body feels is the sea lsystem_coast draws, computed on the CPU and deterministic. cvc::physics floats bodies on it.

O7 is split in three, as O4 was:

  • O7a (this PR): the sea surface and its physics adapter.
  • O7b: the CPU-vs-GPU residual harness, which chooses M.
  • O7c: crates and barrels in lsystem_coast.

cvc/ocean/fft.h

A radix-2 FFT. Its twiddles come from det:: and it is built without FMA contraction, so it gives the same bits on every platform.

cvc/ocean/sea_surface.h

This is lsystem_coast's water vertex shader on the CPU, without the waves too short to move a body.

  • B2, the physics band.

    • Each cascade keeps its lowest band.modes² wavenumbers, with |m| < modes/2. That set is closed under k → −k, so every field it packs is real.

    • The modes evolve as OceanFFT evolves them: h0 e^{iωt} + conj h0(−k) e^{−iωt}, with OceanFFT's dispersion.

    • Three complex FFTs onto a band.grid² lattice give the height + i w, D_x + i D_y and u + i v. Catmull-Rom splines read the lattice between its points.

    • h0 is band-limited per cascade. lsystem_coast's 251 m and 83 m cascades have no modes past |m| = 12, so 32 modes keep them whole, bit for bit. Only the 29 m cascade loses waves to the band: those under 1.8 m.

    • The default is 32 modes on a 64 lattice. O7b chooses M against the residual.

    • Measured cost per step, three cascades, MSVC Release:

      modes / grid ms per step
      32 / 64 0.64
      64 / 64 0.83
      64 / 128 3.2
  • The shader's composition. Each cascade fades with the still depth. The sideways move is capped at 0.8 of the way to the shore. The shore layer adds its swell, and on the beach the swash adds the run-up. wave_amp and chop work like the shader's uniforms.

  • B3, the height over a point. The surface over (x, y) is the point x₀ that the shader moves there: x₀ + D(x₀) = (x, y).

    • The roadmap's fixed point contracts only by |∂D/∂x₀| a step. On a steep test sea (chop 1.5, the whole spectrum down to 6 m waves), its worst miss after 4 steps was 58 cm.
    • This uses Broyden's method instead. The first step is the same, each step costs one evaluation of D, and after that it steps by a secant Jacobian.
    • Worst miss on that sea: 3 cm after 4 steps, 2·10⁻⁵ m after 6, 6·10⁻⁸ m after 8. The default stays at the roadmap's 4.
  • B4, the velocity.

    • Cascades. The time derivatives of their fields at x₀. Below the still level these decay as e^{kz}, with k the cascade's peak wavenumber.
    • Shore layer. Each train's linear finite-depth orbital motion in the local depth (new: shore_layer::velocity). At the surface its vertical velocity is dη/dt.
    • Bores. With bores, a breaking crest also carries the water near the surface shoreward at the phase speed, scaled by its breaking (the surf zone's roller). Linear waves carry nothing over a period; the bore is what carries floating things in.
    • Swash. On the beach it flows up and down the slope at the front's rise over the slope (new: swash_sample::rate), at most √(2gR).
  • Determinism and threads.

    • Everything is computed with det:: and the FFT.
    • The fields for one time are computed once and shared by every read at that time. The cache never changes a value.
    • The class is immutable and safe to read from several threads.
    • Its digest() covers every input: the parameters, each h0 texel, the grid's depth and shore planes, and the shore layer.

cvc/physics/ocean_surface.h

ocean_surface puts a sea_surface behind fluid_surface, so a fluid field's free surface can be the sea.

Tests

  • ocean_sea_test, 16 tests.
    • The FFT is the DFT, in 1-D and 2-D, and goes there and back.
    • The band is the sum of its modes, computed independently from the h0 texels: all six fields, with no crosstalk.
    • The long cascades fit in 32 modes. The velocities are the time derivatives of the fields.
    • On lattice points, the height is the source's height, and the tile repeats. B3's miss after 1, 4, 6 and 8 steps.
    • The cascades' velocity decays with depth.
    • At the shore, the cascades fade and the sideways move stays offshore.
    • The shore layer's vertical velocity at the surface is dη/dt; the bottom stays still and the water moves less below.
    • Breakers carry the water shoreward on average, where linear waves alone carry little.
    • The swash runs up and drains in the right directions, within √(2gR).
    • The digest follows every input, and bad inputs are refused.
    • Four threads read the values one thread reads.
    • Pinned: OceanSea.IsTheSameOnEveryPlatform.
    • A cost report: printed, not checked.
  • physics_field_test, new tests PhysicsOceanSurface.*.
    • The adapter passes the sea's values through, has a digest of its own, and refuses a null sea.
    • A slab rides the open sea: the 251 m cascade under a 9 m/s wind. It moves in step with the surface at its centre (correlation > 0.95), heaves about as high, and floats at half draft about the mean surface.
    • Pinned: PhysicsOceanSurface.CrossPlatformGoldenHash (315e7d5f0aed8af0).

Both pins were measured on Windows. This PR's Linux and macOS jobs are their first cross-platform check.

Checked locally (Windows Release, GL build)

  • ocean_sea_test 16, ocean_shore_test 16 (its pinned digest is unchanged), ocean_spectrum_test.
  • physics_field_test 21, every Physics* gtest (136, 2 skipped by design), physics_replay_test (unchanged), pycvc_physics, cvcgl_physics_scene_binding, cvcgl_ocean_fft.
  • git clang-format is clean.

Next

  • O7b. Read back OceanFFT's displacement in the native harness, compare it with the band at its texels, record the residual's RMS per cascade for several M, and choose M.
  • O7c. Crates and barrels in lsystem_coast that ride the swell, are carried by the breakers and the swash, and settle on calm lake water.

…on it (O7a)

cvc/ocean/fft.h: a deterministic radix-2 FFT (det:: twiddles, no FMA contraction).

cvc/ocean/sea_surface.h: lsystem_coast's water vertex shader on the CPU, without the waves too
short to move a body. Each cascade keeps its lowest band.modes^2 wavenumbers, evolved as OceanFFT
evolves them and transformed onto a band.grid^2 lattice (height + i w, D_x + i D_y, u + i v);
the cascades fade with the still depth, the sideways move is capped at 0.8 of the way to the
shore, and the shore layer and the swash join them. The height over a point is its source's (B3,
by Broyden's method: 3 cm worst after 4 steps on a steep sea where the fixed point misses by
58 cm); the velocity is the cascades' time derivatives decaying with depth, the shore layer's
finite-depth orbital motion (shore_layer::velocity, new) with breakers' bores, and the swash's
run up and down the slope (swash_sample::rate, new).

cvc/physics/ocean_surface.h: a sea_surface behind fluid_surface. A slab rides the open sea in
step with the surface at its centre (correlation > 0.95).

ocean_sea_test (16) and PhysicsOceanSurface (3) cover it; OceanSea.IsTheSameOnEveryPlatform and
PhysicsOceanSurface.CrossPlatformGoldenHash pin values measured on Windows.
@transfix
transfix enabled auto-merge (squash) November 11, 2026 04:12
@transfix
transfix merged commit a75afa0 into master Nov 11, 2026
15 of 33 checks passed
@transfix
transfix deleted the feat/ocean-sea-surface branch November 11, 2026 04:46
transfix added a commit that referenced this pull request Nov 11, 2026
OceanSea.IsTheSameOnEveryPlatform (#1142) fails on both macOS arm64 recipe
lanes (digest 0x1966448ffbcd6035, not 0x21bcce29d45653a8); #1142 merged with
its macOS lanes cancelled, so this is the first time they ran it. cvc::ocean's
sources are built with -ffp-contract=off, but the test computes the sea's sample
points itself (x = 5.0 + 2.6 * i, y = 0.5 + 0.13 * i), and clang contracts those
into FMAs on aarch64. With one rounding instead of two, x differs at i = 23 and
y at i = 29 and 58, so the sea is sampled elsewhere and the digest moves. x86_64
Linux and MSVC do not contract here, so they agree with the Windows value.

Build ocean_sea_test.cpp with -ffp-contract=off (/fp:precise on MSVC), as
nav_potential_test and the dyn plant tests are for the same reason.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant