Skip to content

cvc::physics: a fluid field's free surface, read at a pinned field time (O6a) - #1139

Merged
transfix merged 3 commits into
masterfrom
feat/ocean-fluid-surface
Nov 11, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/ocean-fluid-surface

Conversation

@transfix

@transfix transfix commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

O6a in docs/roadmap/OCEAN-AND-WEATHER-ROADMAP.md: B5's hook on cvc::physics::field and B6's tests. A fluid field gets a free surface, so a floating body rides the waves a renderer draws.

wave_train_surface uses det::exp, cosh and sinh from #1138 (merged), and master is merged in, so the diff is O6a alone.

cvc/physics/fluid_surface.h (decision D2: a virtual interface)

  • fluid_surface has level(x, y), height(x, y, t), velocity(p, t) and digest(). A surface must be deterministic, the same on every platform and immutable while a field holds it. Lakes, streams and the sea are all one type.

  • still_surface is a level with an optional uniform current.

  • wave_train_surface is linear Airy trains on water of one depth (or deep water):

    • the dispersion ω² = g k tanh(k d), solved by Newton from Eckart's start;
    • orbital velocity decaying as cosh(k(z+d)) / sinh(k d), or e^{kz} in deep water;
    • inside a crest, the still level's velocity, since linear theory does not reach higher;
    • det:: throughout.

    The cvc::ocean-backed sea (the physics band's FFT and the shore layer) is O7, as the roadmap's §12 has it.

The field step

  • field::surface is allowed on a fluid only. A medium or a flow with one is refused.

  • In field_set::apply:

    • each cell's top clips to min(box top, height(x, y, t)) over its clipped centre;
    • drag and damping use velocity(p, t) in place of the velocity volumes.

    Nothing else in the loop changed.

The time base, pinned

The roadmap flagged this: world::time() is not the clock's time, since follow() anchors at the clock's tick.

  • world::field_time() is t0 + (tick − tick0)·dt.
  • follow()'s first call sets t0 to the clock's t(). Physics then reads the surface at the time a renderer drawing it at the clock's time uses.
  • set_field_time(t) sets it explicitly. Before either, it equals time().

State

  • With a free surface: save() appends a section (CVCSURF1) with the field-time anchor and each surface's digest, so state_hash() covers both. restore() restores the anchor and refuses a world whose surfaces differ.
  • Without one: the format is unchanged, so every existing hash and physics_replay_test's trace still hold.

Tests (physics_field_test, PhysicsFluidSurface.*, 7 new)

  • Surfaces alone: still_surface and wave_train_surface as Airy says:
    • deep-water k = ω²/g, constant orbital speed Aω at the level, e^{kz} below it;
    • the dispersion relation at finite depth, and no vertical speed at the bottom;
    • a digest that follows every parameter.
  • Refused: a surface on a medium or a flow.
  • Field time: it follows a clock that ran 123.45 s before the world joined it, and set_field_time sets it.
  • B6, calm water: a box at density 500 floats at half draft at two levels, where the surface is, not at the box's top.
  • B6, swell: in a 0.2 m, 4 s swell (25 m wavelength, beside the box's own 0.9 s heave period) the box heaves in phase with the surface at its centre (correlation > 0.97), about as high (amplitude ratio within 15 % of 1), at the swell's period (within 0.12 s).
  • State:
    • runs with one surface hash alike;
    • another amplitude or another field time changes the hash;
    • a save restores into a world holding the same surface, field time included, and the two then step alike;
    • restore refuses another surface, or none.

Still to come:

  • the CPU-vs-GPU residual, which waits for O7's physics band;
  • native-vs-wasm, which rides on the replay lane.

Checked locally (Windows Release, GL build)

  • physics_field_test 17, and every other physics_* test.
  • physics_replay_test (the recorded trace) and cvcgl_physics_scene_binding.
  • git clang-format 18.1.8 is clean on the changed lines.

Next

  • O6b: pycvc bindings for fluid_surface and the field time.
  • O7: the cvc::ocean-backed sea surface (the physics band's 64² CPU FFT, height at a world point by fixed-point iteration, orbital velocity, the shore layer) and debris in lsystem_coast.

…the same on every platform

Before O6 puts the sea into the physics state_hash, h0 and the shore layer must give the same
bits on every platform; they called libm, whose transcendentals each platform rounds its own
way (docs/roadmap/OCEAN-AND-WEATHER-ROADMAP.md, B1's open item).

- cvc::physics's private det_math moves to cvc::algo as cvc/algo/det_math.h (standard library
  only), and gains exp, log, pow, sinh, cosh, acos and hypot. pow carries log x as a double-double
  and multiplies it by y exactly, so it stays within 3 ULP of libm even where y log x is large;
  y = 1, 2, -1 and 0.5 are one correctly rounded operation each. physics/det_math.h is now a
  namespace alias, so the physics step is unchanged (its bit pins and physics_replay_test hold).
- cvc::ocean's spectrum, h0 and shore layer call det:: in place of std::, and are built without
  FMA contraction. cvc::ocean may now include cvc::algo (which depends on nothing).
- algo_det_math_test (was physics_det_math_test) measures each new function against libm over
  the ranges the ocean uses, checks libm's special cases (pow's C99 Annex F table), and pins
  bits. ocean_spectrum_test and ocean_shore_test pin digests of the spectrum, h0 and the shore
  layer's planes, evaluate() and swash(), measured on Windows, for CI to check elsewhere.
…me (O6a)

O6 of docs/roadmap/OCEAN-AND-WEATHER-ROADMAP.md, its B5 hook (decision D2, a virtual
interface) and B6's tests.

- cvc/physics/fluid_surface.h: fluid_surface (level, height, velocity, digest), still_surface
  (a level and a uniform current) and wave_train_surface (linear Airy trains, finite-depth
  dispersion, cosh/sinh decay, det_math throughout so it is the same on every platform).
- field::surface, on a fluid only: field_set::apply clips each cell's top to the surface's
  height over its clipped centre, and drag and damping use the surface's velocity in place of
  the velocity volumes.
- world::field_time(): t0 + (tick - tick0) dt. follow()'s first call anchors it to the clock's
  t(), so physics reads the surface at the time a renderer draws it; set_field_time sets it.
- With a free surface, save() appends a section with the field-time anchor and each surface's
  digest, so state_hash() covers them and restore() refuses other surfaces. Without one the
  format is unchanged: every existing hash and the replay trace hold.
- physics_field_test: half draft on calm water at two levels; heave in a 4 s swell in phase with
  the surface (correlation > 0.97), about as high, at the swell's period; the field time follows
  the clock; hashes agree for one surface and differ for another amplitude or field time; a save
  restores the field time and refuses another surface.
@transfix
transfix enabled auto-merge (squash) November 10, 2026 22:31
@transfix
transfix merged commit ddf8585 into master Nov 11, 2026
15 of 34 checks passed
@transfix
transfix deleted the feat/ocean-fluid-surface branch November 11, 2026 03:17
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