// verify-the-salt-that-barely-moves-the-boil.mjs — the chemistry behind the stratum
// "The Salt That Barely Moves the Boil" (/strata/the-salt-that-barely-moves-the-boil/).
//
// The claim the page corrects: salting pasta water does NOT make it hotter enough to
// matter, and it does NOT make it boil faster. The boiling point goes UP, not down,
// by the colligative law of boiling-point elevation
//
//        ΔTb = i · Kb · b ,
//
// where Kb(water) = 0.512 °C·kg/mol is the ebullioscopic constant, i ≈ 2 is the
// van't Hoff factor for NaCl (it dissociates into Na+ + Cl−), and b is the molality
// (mol solute per kg solvent). A normal cooking dose (≈10 g NaCl per litre) raises
// the boil by less than two tenths of a degree; even a heavy brine barely reaches
// half a degree. And because a higher boiling target means MORE heat is needed to
// reach it, salt cannot make water boil faster — at most it boils negligibly slower,
// the specific-heat drop nearly cancelling the higher target. "Boils faster" is
// backwards.
//
// What this script proves, offline and from scratch, recomputing every number the
// page shows on screen:
//   1. Molar mass of NaCl = 58.44 g/mol from atomic weights.
//   2. ΔTb = i·Kb·b for the page's anchor doses (5, 10, 35 g/L) and the new boiling
//      point 100 + ΔTb — the numbers the slider readout shows.
//   3. The dose-to-degree: how much salt a single °C of elevation would require, and
//      the absurd dose for the popular "makes it much hotter" intuition.
//   4. The time/energy argument: heating 1 L of solution from 20 °C to its boil needs
//      Q = m·c·(Tb − T0). With salt the target Tb rises but the specific heat c drops
//      and the mass m rises; we compute the honest net ratio Q_salt/Q_pure and show it
//      sits within a hair of 1 — i.e. NOT a speedup. The sign of the tiny residual is
//      named, not hidden.
//   5. Monotonicity + a sanity bound (i, Kb match the literature within tolerance).
//
//   run:  node research/the-salt-that-barely-moves-the-boil/verify-the-salt-that-barely-moves-the-boil.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;

// ── 0. constants (the page's stated values) ─────────────────────────────────
const Kb = 0.512;          // °C·kg/mol — ebullioscopic constant of water (CRC / Atkins)
const i_NaCl = 2;          // van't Hoff factor for NaCl (Na+ + Cl−); real ≈1.9
const M_Na = 22.99;        // g/mol
const M_Cl = 35.45;        // g/mol
const M_NaCl = M_Na + M_Cl;
const Tb0 = 100.0;         // °C, boiling point of pure water at 1 atm

console.log('\n— molar mass of NaCl —');
console.log(`  M(NaCl) = ${M_Na} + ${M_Cl} = ${M_NaCl.toFixed(2)} g/mol`);
ok('M(NaCl) = 58.44 g/mol', approx(M_NaCl, 58.44, 1e-9), `${M_NaCl.toFixed(2)} g/mol`);
ok('Kb(water) = 0.512 °C·kg/mol (textbook)', approx(Kb, 0.512, 1e-9));
ok('i(NaCl) = 2 ideal (real ≈1.9, the page names this)', i_NaCl === 2);

// ── 1. molality from grams-per-litre (1 L water ≈ 1 kg solvent basis) ────────
// The page's slider gives grams of salt per litre of water. Molality b = (g/M) / 1 kg.
const molality = (gPerL) => (gPerL / M_NaCl) / 1.0;     // mol/kg
// boiling-point elevation
const deltaTb = (gPerL) => i_NaCl * Kb * molality(gPerL);
const boilAt  = (gPerL) => Tb0 + deltaTb(gPerL);

console.log('\n— ΔTb = i·Kb·b at the page anchor doses —');
const doses = [5, 10, 35];
const expectDT = { 5: 0.0876, 10: 0.1752, 35: 0.6133 };  // hand-computed targets
for (const g of doses) {
  const b = molality(g), dT = deltaTb(g), Tb = boilAt(g);
  console.log(`  ${String(g).padStart(2)} g/L → b = ${b.toFixed(4)} mol/kg` +
    `, ΔTb = ${dT.toFixed(4)} °C, boils at ${Tb.toFixed(4)} °C`);
  ok(`ΔTb(${g} g/L) ≈ ${expectDT[g]} °C`, approx(dT, expectDT[g], 0.001), `${dT.toFixed(4)} °C`);
}
// the headline: a heaped-teaspoon dose moves the boil under 0.2 °C
ok('a 10 g/L cooking dose raises the boil by < 0.2 °C', deltaTb(10) < 0.2,
   `ΔTb = ${deltaTb(10).toFixed(3)} °C`);
ok('even a heavy 35 g/L brine stays under 0.7 °C', deltaTb(35) < 0.7,
   `ΔTb = ${deltaTb(35).toFixed(3)} °C`);
// direction: elevation is strictly positive — the boil goes UP, never down
let monoUp = true, prev = -1;
for (const g of [0, 5, 10, 20, 35, 100]) { const v = deltaTb(g); if (v < prev) monoUp = false; prev = v; }
ok('ΔTb is non-negative and increases with dose (boil goes UP, not down)', monoUp && deltaTb(10) > 0);

// ── 2. how much salt would ONE whole degree take? (the intuition's price) ────
// Solve i·Kb·(g/M) = 1  ⇒  g = M / (i·Kb).
const gramsForOneDegree = M_NaCl / (i_NaCl * Kb);
console.log('\n— how much salt for a single °C of elevation —');
console.log(`  g = M/(i·Kb) = ${M_NaCl.toFixed(2)}/(${i_NaCl}·${Kb}) = ${gramsForOneDegree.toFixed(1)} g per litre`);
ok('≈57 g/L (≈ 11 teaspoons) needed for +1 °C', approx(gramsForOneDegree, 57.07, 0.1),
   `${gramsForOneDegree.toFixed(1)} g/L`);
// near-saturation (~360 g/L) ceiling — even drowning the pot barely clears 6 °C
const dT_saturated = deltaTb(360);
console.log(`  at near-saturation 360 g/L (ideal i=2): ΔTb = ${dT_saturated.toFixed(2)} °C (and i actually droops, so real is less)`);
ok('even near-saturation (ideal) stays a single-digit elevation', dT_saturated < 7,
   `${dT_saturated.toFixed(2)} °C`);

// ── 3. teaspoon sanity: ~6 g NaCl per level teaspoon ────────────────────────
// The page calls 10 g "a heaped teaspoon"; a level teaspoon of fine salt ≈ 6 g.
const TSP_G = 6;          // g per level teaspoon of fine table salt (≈5 mL × 1.2 g/mL)
const tspFor10 = 10 / TSP_G;
console.log('\n— teaspoon sanity —');
console.log(`  10 g ÷ ${TSP_G} g/tsp = ${tspFor10.toFixed(2)} tsp  (≈ a heaped teaspoon)`);
ok('10 g ≈ 1.5–2 level teaspoons (a heaped one)', tspFor10 > 1.3 && tspFor10 < 2.0,
   `${tspFor10.toFixed(2)} tsp`);

// ── 4. the time-to-boil argument: energy Q = m·c·(Tb − T0) ───────────────────
// The popular claim is "salt makes it boil FASTER." For the SAME burner power, time ∝
// the energy Q needed to bring a fixed VOLUME (you fill a pot to a line) from T0 to its
// boil. With salt three things change vs pure water, two of them OPPOSING the third:
//   (+) the target Tb rises by ΔTb            → needs MORE energy
//   (−) the specific heat c drops with salt    → needs LESS energy per gram
//   (+) the mass per litre rises (denser)      → needs MORE energy
// We use transparent, sourced linear coefficients and report the honest net ratio.
// Pure water reference: density 1.000 g/mL, c = 4.182 J/g·K. (CRC, ~20–100 °C avg.)
const T0 = 20.0;                  // °C start
const c_pure = 4.182;             // J/g·K
const rho_pure = 1.000;           // g/mL  → 1000 g per litre

// salt-water properties as functions of mass-fraction w (kg salt / kg solution):
//   density:  rho ≈ 1.000 + 0.70·w  g/mL  (≈ +0.7% per 1% salt; seawater 3.5% → 1.025)
//   spec heat: c ≈ c_pure·(1 − 1.5·w)      (seawater 3.5% → ~0.93×, matches Millero 1973)
function saltProps(gPerL) {
  // grams salt per litre of WATER → convert to mass fraction of the SOLUTION.
  const mWater = 1000;                 // g water per litre of water
  const mSalt = gPerL;                 // g salt added
  const mSol = mWater + mSalt;         // g solution
  const w = mSalt / mSol;              // mass fraction
  const rho = 1.000 + 0.70 * w;        // g/mL of solution
  const c = c_pure * (1 - 1.5 * w);    // J/g·K
  return { w, rho, c, mSol };
}
// energy to bring ONE LITRE of solution to its boil from T0:
//   mass in 1 L = rho(g/mL)·1000 mL ; ΔT_heat = (Tb − T0)
function energyPerLitre(gPerL) {
  if (gPerL === 0) return rho_pure * 1000 * c_pure * (Tb0 - T0);
  const p = saltProps(gPerL);
  const Tb = boilAt(gPerL);
  const mass = p.rho * 1000;           // g in one litre of solution
  return mass * p.c * (Tb - T0);
}

console.log('\n— time-to-boil (energy) for ONE LITRE, burner power fixed —');
const Q_pure = energyPerLitre(0);
console.log(`  pure water:  Q = ${(Q_pure / 1000).toFixed(2)} kJ  (target ${Tb0.toFixed(2)} °C)`);
for (const g of [10, 35]) {
  const Q = energyPerLitre(g);
  const ratio = Q / Q_pure;
  const Tb = boilAt(g);
  const sign = ratio > 1 ? 'slower' : (ratio < 1 ? 'faster' : 'same');
  console.log(`  ${String(g).padStart(2)} g/L:    Q = ${(Q / 1000).toFixed(2)} kJ  (target ${Tb.toFixed(2)} °C)` +
    `  →  ${(ratio * 100).toFixed(2)}% of pure  (${sign}, by ${Math.abs(ratio - 1) * 100 < 0.005 ? '<0.005' : (Math.abs(ratio - 1) * 100).toFixed(2)}%)`);
}
// the load-bearing honest claims. The NET time-to-boil for a fixed volume is a near-
// wash: the higher target (toward slower) is nearly cancelled by the lower specific
// heat (toward faster). The page does NOT claim a direction for the net — it claims
// the net is NEGLIGIBLE and MODEL-DEPENDENT, the opposite of the dramatic speedup of
// folklore. What it DOES claim with certainty is the colligative direction.
const r10 = energyPerLitre(10) / Q_pure;
const r35 = energyPerLitre(35) / Q_pure;
ok('a 10 g/L dose changes the energy-to-boil by under 1% (a near-wash, not a speedup)',
   Math.abs(r10 - 1) < 0.01, `ratio = ${r10.toFixed(4)} (${((r10 - 1) * 100).toFixed(2)}%)`);
ok('a 35 g/L brine changes it by under 3% — within stove/lid/measurement noise',
   Math.abs(r35 - 1) < 0.03, `ratio = ${r35.toFixed(4)} (${((r35 - 1) * 100).toFixed(2)}%)`);
// the two effects genuinely oppose: target up (slower) vs specific-heat down (faster).
const energyFromTargetOnly10 = (boilAt(10) - T0) / (Tb0 - T0);   // pure-water mass & c
ok('the boiling-target effect ALONE always pushes toward MORE energy (slower) — the certain direction',
   energyFromTargetOnly10 > 1, `(Tb−T0) ratio = ${energyFromTargetOnly10.toFixed(4)}`);
ok('the specific-heat effect ALONE pushes the OTHER way (less energy per gram) — why the net is a wash',
   saltProps(10).c < c_pure, `c(10 g/L) = ${saltProps(10).c.toFixed(3)} < ${c_pure}`);
// honest: the net SIGN depends on the c(w) coefficient (literature scatter). Show that
// a milder, equally-defensible c-slope flips the net toward slower — proving the page
// is right NOT to assert a net direction, only that the magnitude is negligible.
function energyMildC(gPerL) {
  if (gPerL === 0) return Q_pure;
  const p = saltProps(gPerL);
  const cMild = c_pure * (1 - 0.8 * (p.w));     // gentler specific-heat slope
  return p.rho * 1000 * cMild * (boilAt(gPerL) - T0);
}
const rMild10 = energyMildC(10) / Q_pure;
ok('with a gentler (also-defensible) specific-heat slope the net flips to SLOWER — so the net direction is genuinely model-dependent',
   rMild10 > 1 && r10 < 1, `gentle = ${rMild10.toFixed(4)} vs steep = ${r10.toFixed(4)}`);
ok('either way the net stays within ~1% of pure water (the only robust time claim: negligible)',
   Math.abs(rMild10 - 1) < 0.01 && Math.abs(r10 - 1) < 0.01,
   `gentle ${((rMild10 - 1) * 100).toFixed(2)}% / steep ${((r10 - 1) * 100).toFixed(2)}%`);

// ── 5. literature sanity: real i for NaCl ≈ 1.9 (we state ideal 2) ───────────
console.log('\n— literature sanity —');
const i_real = 1.9;
const dT_real10 = i_real * Kb * molality(10);
console.log(`  with real i = ${i_real}: ΔTb(10 g/L) = ${dT_real10.toFixed(4)} °C (vs ${deltaTb(10).toFixed(4)} ideal)`);
ok('real i=1.9 makes the elevation even smaller — the page over-, not under-states it',
   dT_real10 < deltaTb(10), `${dT_real10.toFixed(4)} < ${deltaTb(10).toFixed(4)}`);

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