// verify-the-ice-that-pressure-didnt-melt.mjs — the physics behind the stratum
// "The Ice That Pressure Didn't Melt" (/strata/the-ice-that-pressure-didnt-melt/).
//
// The claim the page corrects: the schoolbook says ice skates glide because the
// skater's weight raises the pressure under the blade enough to MELT the ice into
// a lubricating film (pressure-melting / regelation). Run the actual numbers along
// the real ice-water phase line and the pressure melts almost nothing — the melting
// point drops about half a degree, not the 5-10 degrees a cold rink would require.
// The slippery film is real, but it comes from surface PREMELTING and FRICTIONAL
// HEATING, not from the skater's weight.
//
// The governing law is the Clausius-Clapeyron relation on the SOLID-LIQUID line:
//
//        dT/dP = T * (v_liquid - v_solid) / L_f
//
// where v = 1/rho is specific volume and L_f is the latent heat of fusion. Because
// ice is LESS dense than water (v_liquid < v_solid), the slope is NEGATIVE: squeezing
// ice lowers its melting point. But the volume change is tiny, so the slope is tiny.
//
// What this script proves, offline and from scratch, recomputing every number the
// page shows on screen:
//   1. The phase-line slope dT/dP = -0.0743 K/MPa (= -13.5 MPa/K inverse), from the
//      real densities of ice and water and the latent heat of fusion. (NOT -134 MPa/K
//      -- an order-of-magnitude error that floats around; the correct figure is -13.5.)
//   2. The skater's contact pressure: 70 kg on a 1 cm^2 blade contact -> ~6.9 MPa.
//   3. The melting-point depression that pressure buys: dT/dP * dP = about -0.51 K.
//      Nowhere near the -5 to -10 C of a typical rink. Pressure-melting is ruled out.
//   4. A literature cross-check: 500 N skater on 150e-6 m^2 -> 3.3 MPa -> -0.24 K,
//      the figure quoted in standard treatments. Our slope reproduces it.
//   5. The mechanism that DOES close the gap: frictional heating. A skater gliding
//      at speed v with kinetic friction mu dissipates power P_fric = mu * m * g * v
//      into a tiny contact patch -- tens of watts into ~1 cm^2, a heat flux that
//      drives the contact to the melting point and makes the lubricating film.
//
//   run:  node research/the-ice-that-pressure-didnt-melt/verify-the-ice-that-pressure-didnt-melt.mjs
//
// No dependencies. Every number the page displays is recomputed here, the same way.

let fails = 0;
const ok = (name, cond, extra = '') => {
  if (!cond) fails++;
  console.log(`${cond ? '  ok' : 'FAIL'}  ${name}${extra ? '   ' + extra : ''}`);
};
const approx = (a, b, eps) => Math.abs(a - b) <= eps;

// ── physical constants (the page uses these exact values) ───────────────────
const T_MELT   = 273.15;     // K   melting point of ice at 1 atm
const RHO_ICE  = 916.7;      // kg/m^3  density of ice Ih at 0 C
const RHO_WATER = 999.84;    // kg/m^3  density of liquid water at 0 C
const L_FUSION = 333550;     // J/kg   latent heat of fusion of ice
const G        = 9.80665;    // m/s^2  standard gravity

// ── 1. Clausius-Clapeyron slope of the ice-water line ───────────────────────
// dT/dP = T * (v_liquid - v_solid) / L_f,  v = 1/rho.
const v_ice   = 1 / RHO_ICE;
const v_water = 1 / RHO_WATER;
const dv      = v_water - v_ice;             // m^3/kg, NEGATIVE (water is denser)
const dTdP    = (T_MELT * dv) / L_FUSION;    // K/Pa
const dTdP_MPa = dTdP * 1e6;                 // K/MPa
const dPdT_MPa = 1 / dTdP_MPa;              // MPa/K (the inverse)
console.log('\n— Clausius-Clapeyron slope of the ice-water phase line —');
console.log(`  dv = v_water - v_ice = ${dv.toExponential(4)} m^3/kg  (negative: ice is less dense)`);
console.log(`  dT/dP = ${dTdP_MPa.toFixed(5)} K/MPa`);
console.log(`  dP/dT = ${dPdT_MPa.toFixed(2)} MPa/K  (the inverse)`);
ok('slope dT/dP = -0.0743 K/MPa', approx(dTdP_MPa, -0.0743, 0.0005), `${dTdP_MPa.toFixed(4)} K/MPa`);
ok('inverse dP/dT = -13.5 MPa/K (NOT -134)', approx(dPdT_MPa, -13.46, 0.1), `${dPdT_MPa.toFixed(2)} MPa/K`);
// the slope is negative -- pressure LOWERS the melting point
ok('slope is negative (squeezing ice lowers its melting point)', dTdP_MPa < 0);

// ── 2. The skater's contact pressure ────────────────────────────────────────
// P = F / A = m*g / A. The page's default: 70 kg on a 1 cm^2 blade contact.
function pressureMPa(massKg, areaCm2) {
  const A = areaCm2 * 1e-4;          // cm^2 -> m^2
  return (massKg * G / A) / 1e6;     // Pa -> MPa
}
const P_skater = pressureMPa(70, 1.0);
console.log('\n— skater contact pressure: 70 kg on 1 cm^2 blade contact —');
console.log(`  P = m*g/A = 70 * ${G} / 1e-4 = ${P_skater.toFixed(3)} MPa`);
ok('pressure ~ 6.9 MPa', approx(P_skater, 6.86, 0.05), `${P_skater.toFixed(2)} MPa`);

// ── 3. The melting-point depression that pressure buys ──────────────────────
// dT = (dT/dP) * dP, with dP the over-pressure above atmospheric (~0.1 MPa, negligible
// here; the page takes dP = P_skater since contact pressure >> atmospheric).
function meltDepressionK(pressureMPa) { return dTdP_MPa * pressureMPa; }   // K (negative)
const dT_skater = meltDepressionK(P_skater);
console.log('\n— melting-point depression from that pressure —');
console.log(`  dT = ${dTdP_MPa.toFixed(5)} K/MPa * ${P_skater.toFixed(2)} MPa = ${dT_skater.toFixed(3)} K`);
ok('melting point drops only ~0.51 K', approx(dT_skater, -0.51, 0.02), `${dT_skater.toFixed(3)} K`);
// the verdict: this falls FAR short of a typical rink temperature
const RINK_C = [-5, -7, -10];   // typical rink temperatures, deg C
for (const Trink of RINK_C) {
  // pressure-melting would need the depression to reach Trink (i.e. |dT| >= |Trink|)
  const reaches = Math.abs(dT_skater) >= Math.abs(Trink);
  const shortfall = Math.abs(Trink) - Math.abs(dT_skater);
  console.log(`  rink at ${Trink} C: pressure-melting ${reaches ? 'WOULD' : 'does NOT'} reach it (short by ${shortfall.toFixed(2)} K)`);
  ok(`pressure-melting fails at ${Trink} C rink (short by >${(Math.abs(Trink)-1).toFixed(0)} K)`,
     !reaches && shortfall > Math.abs(Trink) - 1, `short by ${shortfall.toFixed(2)} K`);
}
// even an absurdly small contact area can't save it at -10 C
const P_extreme = pressureMPa(70, 0.1);    // 70 kg on 0.1 cm^2 -- a knife edge
const dT_extreme = meltDepressionK(P_extreme);
console.log(`\n  even 70 kg on a 0.1 cm^2 knife-edge -> ${P_extreme.toFixed(1)} MPa -> ${dT_extreme.toFixed(2)} K`);
ok('even 0.1 cm^2 contact does not melt a -10 C rink', Math.abs(dT_extreme) < 10,
   `${dT_extreme.toFixed(2)} K (still short of -10 C)`);

// ── 4. Literature cross-check: 500 N on 150e-6 m^2 -> 3.3 MPa -> -0.24 K ─────
console.log('\n— literature cross-check (500 N skater, 150e-6 m^2 contact) —');
const P_lit = (500 / 150e-6) / 1e6;        // MPa
const dT_lit = meltDepressionK(P_lit);
console.log(`  P = 500 N / 150e-6 m^2 = ${P_lit.toFixed(2)} MPa  ->  dT = ${dT_lit.toFixed(3)} K`);
ok('cross-check pressure ~ 3.3 MPa', approx(P_lit, 3.33, 0.05), `${P_lit.toFixed(2)} MPa`);
ok('cross-check depression ~ -0.24 K (matches the literature)', approx(dT_lit, -0.247, 0.01),
   `${dT_lit.toFixed(3)} K`);

// ── 5. The mechanism that closes the gap: frictional heating ────────────────
// A skater gliding at speed v against kinetic friction mu dissipates
//   P_fric = mu * m * g * v   watts,
// nearly all of it into the contact patch. Even with the very low ice friction
// coefficients (~0.003-0.01), a few m/s puts tens of watts into ~1 cm^2 -- a heat
// flux that drives the contact to 0 C and makes the lubricating film.
function frictionPowerW(mu, massKg, vMs) { return mu * massKg * G * vMs; }
console.log('\n— the mechanism that works: frictional heating —');
const mu = 0.006, mass = 70, v = 6;        // representative glide
const Pf = frictionPowerW(mu, mass, v);
const Acm2 = 1.0;                          // contact area cm^2 (placeholder, replaced below)
const flux = Pf / (1.0 * 1e-4);            // W/m^2 over 1 cm^2
console.log(`  P_fric = mu*m*g*v = ${mu} * ${mass} * ${G} * ${v} = ${Pf.toFixed(2)} W`);
console.log(`  over a 1 cm^2 contact that is ${(flux/1e3).toFixed(0)} kW/m^2 of heat into the ice surface`);
ok('frictional power is tens of watts (order 10-100 W)', Pf > 10 && Pf < 100, `${Pf.toFixed(1)} W`);
ok('heat flux into 1 cm^2 is hundreds of kW/m^2', flux > 1e5, `${(flux/1e3).toFixed(0)} kW/m^2`);
// the honest comparison: pressure-melting depresses the melting point ~0.5 K, while
// frictional heating delivers a flux that easily reaches the melting point -- the
// SIGN of the argument is what the page proves; the exact film physics is contested.
const ratio = Math.abs(RINK_C[2]) / Math.abs(dT_skater);  // how far short pressure falls at -10 C
console.log(`\n  pressure-melting falls short of a -10 C rink by a factor of ~${ratio.toFixed(0)}x`);
ok('pressure-melting is ~20x too weak at a -10 C rink', ratio > 15 && ratio < 25, `${ratio.toFixed(1)}x`);

console.log(`\n${fails === 0 ? 'ALL CHECKS PASSED' : fails + ' CHECK(S) FAILED'}\n`);
process.exit(fails === 0 ? 0 : 1);
