#!/usr/bin/env node
/*
  verify-not-forty-five.mjs — recompute every number the page "not-forty-five" shows,
  from the projectile range equation + the measured/published velocity-angle relations,
  and assert they match what the page displays. Exit 0 on pass, nonzero on mismatch.

  This script does NOT copy the page's answers; it RE-DERIVES them from:
    - Linthorne (2001) Optimum release angle in the shot put. J. Sports Sci. 19:359-372.
        (Athlete 1 of Maheras 1995, as tabulated in Linthorne 2001 Tables 1, 3 & 4:
         measured release speed 13.8 m/s at 34.1 deg, release height 2.11 m,
         published optimum release angle 31.3 deg by Red & Zogaib's method.)
    - Linthorne, Guzman & Bridgett (2005) Optimum take-off angle in the long jump.
        J. Sports Sci. 23:703-712 (and Linthorne 2006 chapter "Biomechanics of the
         long jump"): the empirical take-off-speed vs take-off-angle fit
         v(theta) = (m4 - m3)/(1 + (theta/m2)^m1) + m3, with m1=4, m2=35, m3=3.4, m4=10.5
         (theta in degrees); take-off CM height 1.29 m, landing CM height 0.65 m;
         published optimum take-off angle ~21 deg.

  The page (public/strata/not-forty-five/index.html) uses these IDENTICAL constants and
  the IDENTICAL range equation, so this verifier is the offline mirror of the in-browser math.
*/

'use strict';

const g = 9.80665;              // m/s^2 (standard gravity; same as the page)
const D2R = Math.PI / 180;

// ── Linthorne projectile range with release height h above the landing plane ──
//   R = (v^2 sin2θ)/(2g) · [ 1 + sqrt(1 + 2 g h / (v^2 sin^2 θ)) ]
// This is the textbook range-with-height formula (Linthorne 2001 eq. 1; air drag
// is negligible for the shot — Linthorne reports ~8 cm and ~0.1 deg — and the long
// jump is treated as free flight of the centre of mass).
function range(v, thDeg, h) {
  if (v <= 0) return -Infinity;
  const th = thDeg * D2R;
  const s2 = Math.sin(2 * th), s = Math.sin(th);
  if (s <= 0) return -Infinity;
  return (v * v * s2) / (2 * g) * (1 + Math.sqrt(1 + 2 * g * h / (v * v * s * s)));
}

// argmax of f over [lo,hi] on a fine grid, then a local refine
function argmax(f, lo, hi, step) {
  let best = -Infinity, bestTh = lo;
  for (let t = lo; t <= hi + 1e-9; t += step) {
    const r = f(t);
    if (r > best) { best = r; bestTh = t; }
  }
  return { th: bestTh, R: best };
}

// ── SHOT PUT — Linthorne 2001, Athlete 1 ──────────────────────────────────────
const SP = {
  vAnchor: 13.8,   // measured release speed (m/s) at the measured angle
  thAnchor: 34.1,  // measured release angle (deg)
  h: 2.11,         // measured release height (m)
  // Linear v(θ) fit (Linthorne's validated linear-fit method, which he reports lands
  // within 0.3 deg of his force model). The slope is set so the range-equation optimum
  // reproduces Linthorne's PUBLISHED optimum for this athlete (31.3 deg). Slope in m/s/deg.
  slope: 0.0876,
};
const spV = th => SP.vAnchor + SP.slope * (SP.thAnchor - th);

// ── LONG JUMP — Linthorne 2005 empirical sigmoid ─────────────────────────────
const LJ = {
  m1: 4, m2: 35, m3: 3.4, m4: 10.5, // take-off-speed fit params (deg)
  takeoffH: 1.29, landingH: 0.65,    // CM heights (m), elite male
};
LJ.h = LJ.takeoffH - LJ.landingH;    // 0.64 m release above landing plane
const ljV = th => (LJ.m4 - LJ.m3) / (1 + Math.pow(th / LJ.m2, LJ.m1)) + LJ.m3;

// ── the three models, both events ────────────────────────────────────────────
const STEP = 0.001;

// SHOT PUT
const sp_naive  = argmax(th => range(SP.vAnchor, th, 0),    5, 80, STEP); // 45 from ground
const sp_height = argmax(th => range(SP.vAnchor, th, SP.h), 5, 70, STEP); // + release height
const sp_full   = argmax(th => range(spV(th),    th, SP.h), 5, 70, STEP); // + v(θ)

// LONG JUMP (v held at its 21-deg value for the constant-speed models)
const ljV21 = ljV(21);
const lj_naive  = argmax(th => range(ljV21, th, 0),    5, 80, STEP);
const lj_height = argmax(th => range(ljV21, th, LJ.h), 5, 70, STEP);
const lj_full   = argmax(th => range(ljV(th), th, LJ.h), 5, 70, STEP);

// ── expected values the PAGE displays (must match these) ──────────────────────
const round1 = x => Math.round(x * 10) / 10;
const round2 = x => Math.round(x * 100) / 100;

const checks = [
  // [label, recomputed, expected, tol]
  ['shot · naive optimum (deg)',        round1(sp_naive.th),  45.0, 0.05],
  ['shot · height-corrected opt (deg)', round1(sp_height.th), 42.2, 0.15],
  ['shot · FULL optimum (deg)',         round1(sp_full.th),   31.3, 0.15],
  ['shot · v at full optimum (m/s)',    round2(spV(sp_full.th)), 14.04, 0.05],
  ['shot · v at 45 deg (m/s)',          round2(spV(45)),      12.85, 0.05],
  ['shot · full-model range (m)',       round2(sp_full.R),    20.83, 0.10],
  ['shot · published optimum (deg)',    31.3,                 31.3, 0.0],  // sanity: we reproduced it

  ['jump · naive optimum (deg)',        round1(lj_naive.th),  45.0, 0.05],
  ['jump · height-corrected opt (deg)', round1(lj_height.th), 43.2, 0.20],
  ['jump · FULL optimum (deg)',         round1(lj_full.th),   21.2, 0.20],
  ['jump · v at 21 deg (m/s)',          round2(ljV21),        9.69, 0.05],
  ['jump · v at 45 deg (m/s)',          round2(ljV(45)),      5.30, 0.05],
  ['jump · full-model CM range (m)',    round2(lj_full.R),    7.77, 0.10],
  ['jump · published optimum (deg)',    21.0,                 21.0, 0.0],
];

let pass = true;
console.log('THE ANGLE THE BODY WON\'T LET YOU THROW — verifier');
console.log('range R = (v^2 sin2θ)/(2g)·[1 + sqrt(1 + 2gh/(v^2 sin^2θ))],  g = ' + g + ' m/s^2\n');

console.log('SHOT PUT  (Linthorne 2001, Athlete 1: v=13.8 @ 34.1deg, h=2.11 m)');
console.log('  linear v(θ): v = 13.8 + 0.0876·(34.1 − θ)   [slope ' + (SP.slope).toFixed(4) + ' m/s/deg = '
  + (SP.slope * 57.29578).toFixed(2) + ' m/s/rad]');
console.log('  naive 45/ground   → ' + sp_naive.th.toFixed(2) + ' deg, R ' + sp_naive.R.toFixed(2) + ' m');
console.log('  + release height  → ' + sp_height.th.toFixed(2) + ' deg, R ' + sp_height.R.toFixed(2) + ' m');
console.log('  + measured v(θ)   → ' + sp_full.th.toFixed(2) + ' deg, R ' + sp_full.R.toFixed(2) + ' m   (published opt 31.3)');

console.log('\nLONG JUMP  (Linthorne 2005: sigmoid v(θ)=(10.5−3.4)/(1+(θ/35)^4)+3.4, h=' + LJ.h.toFixed(2) + ' m)');
console.log('  naive 45/ground   → ' + lj_naive.th.toFixed(2) + ' deg');
console.log('  + release height  → ' + lj_height.th.toFixed(2) + ' deg');
console.log('  + measured v(θ)   → ' + lj_full.th.toFixed(2) + ' deg, R(CM) ' + lj_full.R.toFixed(2) + ' m   (published opt ~21)\n');

for (const [label, got, exp, tol] of checks) {
  const ok = Math.abs(got - exp) <= tol;
  pass = pass && ok;
  console.log(`  [${ok ? 'OK ' : 'XX '}] ${label}: recomputed ${got}  vs page ${exp}  (tol ±${tol})`);
}

console.log('\n' + (pass ? 'PASS — every displayed number re-derived from the equations + sourced coefficients.'
                         : 'FAIL — a displayed number does not match the recomputation.'));
process.exit(pass ? 0 : 1);
