#!/usr/bin/env node
// verify.mjs — every number the Coriolis-drain stratum shows, recomputed from
// first principles and asserted. Run: `node research/coriolis-drain/verify.mjs`.
//
// The claim the page settles (narrow and sturdy): the Coriolis effect is real
// and *governs* large slow systems (hurricanes), but in a sink it is buried
// ~10^4–10^5 deep under leftover motion — so which way your basin drains is set
// by how you filled it, not by the hemisphere. The only honest exception is a
// lab tuned to remove the leftover motion (Shapiro 1962; Trefethen et al. 1965).
//
// Sources for the cited (not computed) facts:
//   Shapiro, A. H. "Bath-Tub Vortex." Nature 196, 1080–1081 (1962).  [MIT, ~42°N]
//   Trefethen, L. M.; Bilger, R. W.; Fink, P. T.; Luxton, R. E.; Tanner, R. I.
//     "The Bath-Tub Vortex in the Southern Hemisphere." Nature 207, 1084–1085 (1965). [Sydney, ~34°S]

let pass = 0, fail = 0;
const approx = (a, b, rel = 0.02) => Math.abs(a - b) <= Math.abs(b) * rel;
function check(name, got, want, rel) {
  const ok = approx(got, want, rel);
  console.log(`${ok ? 'ok  ' : 'FAIL'}  ${name}: ${got.toPrecision(5)}  (expect ≈ ${typeof want === 'number' ? want.toPrecision(5) : want})`);
  ok ? pass++ : fail++;
}
function assert(name, cond) {
  console.log(`${cond ? 'ok  ' : 'FAIL'}  ${name}`);
  cond ? pass++ : fail++;
}

// ── Earth's rotation ────────────────────────────────────────────────────────
const SIDEREAL_DAY = 86164.0905;            // s (one rotation relative to the stars)
const OMEGA = 2 * Math.PI / SIDEREAL_DAY;   // rad/s
check('Omega (Earth angular velocity, rad/s)', OMEGA, 7.2921e-5, 1e-3);

// Coriolis parameter f = 2 Ω sin(latitude). The dynamically relevant quantity.
const f = (latDeg) => 2 * OMEGA * Math.sin(latDeg * Math.PI / 180);
check('f at 45 N (1/s)', f(45), 1.0313e-4, 1e-3);
check('f at MIT ~42.4 N (1/s)', f(42.4), 9.835e-5, 2e-3);
check('f at Sydney ~33.9 S (magnitude, 1/s)', Math.abs(f(-33.9)), 8.135e-5, 2e-3);
assert('f vanishes at the equator (Coriolis = 0 there, exactly)', Math.abs(f(0)) < 1e-12);
assert('f is northern-positive, southern-negative (the sign that flips the spin)', f(20) > 0 && f(-20) < 0);

// ── The Rossby number Ro = U / (f L): inertia vs Coriolis ────────────────────
// Ro << 1  → Coriolis governs the flow.   Ro >> 1 → Coriolis is negligible.
const Ro = (U, L, latDeg = 45) => U / (Math.abs(f(latDeg)) * L);

const systems = [
  // name,                  L (m),   U (m/s),  note
  ['Teacup being stirred',   0.07,   0.10],
  ['Bathroom sink',          0.30,   0.30],
  ['Bathtub',                0.70,   0.30],
  ['Flushing toilet',        0.30,   0.50],
  ["Shapiro's settled tank", 1.80,   0.001],   // 6-ft tank, ~1 mm/s creep after 24 h settling
  ['Dust devil',            10,      8],
  ['Tornado',              300,     50],
  ['Hurricane',         500000,     30],
  ['Ocean gyre',       3000000,     0.1],
];
console.log('\n  system                     L(m)      U(m/s)     Ro@45°');
for (const [name, L, U] of systems) {
  const r = Ro(U, L);
  console.log(`  ${name.padEnd(26)} ${String(L).padStart(8)} ${String(U).padStart(9)}   ${r.toExponential(2)}`);
}

// The headline contrasts: hurricane order ~1 (governs), sink ~10^4 (ignored).
const RoSink = Ro(0.30, 0.30);
const RoHurricane = Ro(30, 500000);
check('Ro for a bathroom sink (≈10^4)', RoSink, 9700, 0.05);
check('Ro for a hurricane (order 1)', RoHurricane, 0.58, 0.05);
assert('a sink is ~17,000× further from "Coriolis governs" than a hurricane',
       RoSink / RoHurricane > 1e4);
assert('Coriolis governs the hurricane (Ro ≲ 1) but not the sink (Ro ≫ 1)',
       RoHurricane < 3 && RoSink > 1000);

// ── How buried is it? Coriolis acceleration vs the swirl you actually see ─────
// Coriolis accel on draining water:  a_c = 2 Ω U sin(lat).
const aCoriolisSink = 2 * OMEGA * 0.30 * Math.sin(45 * Math.PI / 180);
check('Coriolis acceleration in a sink (m/s^2)', aCoriolisSink, 3.094e-5, 1e-3);

// The visible bathtub vortex: tangential v ~0.3 m/s at radius ~0.05 m near the drain.
const aVortex = 0.30 * 0.30 / 0.05;   // v^2 / r
check('centripetal accel of the visible vortex (m/s^2)', aVortex, 1.8, 1e-3);
const buriedFactor = aVortex / aCoriolisSink;
check('the vortex you see is this many × stronger than the Coriolis nudge', buriedFactor, 58000, 0.05);
assert('the swirl in your sink is ≥10,000× the planet’s nudge → it is leftover motion, not Earth’s spin',
       buriedFactor > 1e4);

// ── The lab exception, stated honestly (cited facts, not computed) ────────────
// Shapiro removed the leftover motion (not the Rossby number): a symmetric 6-ft
// tank, covered against drafts, left to settle 24 h, drained slowly through a
// 3/8-inch hole over ~20 min. For the first 12–15 min the float held still;
// then it crept counterclockwise, peaking near one turn every 3–4 s.
// Trefethen et al. repeated it in Sydney → clockwise. Both directions are the
// SAME physics: the hemisphere sets the sign once everything louder is silenced.
assert('the exception is real and sign-flipped by hemisphere (N: CCW, S: CW)', f(42.4) > 0 && f(-33.9) < 0);

console.log(`\n${fail === 0 ? 'ALL GREEN' : 'SOME FAILED'} — ${pass} passed, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);
