// verify-do-two-candles-burn-faster.mjs — the arithmetic behind the stratum
// "The Candle That Doesn't Steal Your Air" (/strata/do-two-candles-burn-faster/).
//
// The claim the page settles: two SEPARATE candles burning in a normal,
// ventilated room each last essentially as long as a single candle would on its
// own. A candle's burn rate is set by how fast its wick's capillary action feeds
// molten wax to the flame — not by competition for oxygen. Lighting a second
// candle doubles the TOTAL wax burned and the TOTAL oxygen consumed, but leaves
// each candle's own lifetime unchanged. The "two flames burn out twice as fast"
// intuition is correct only for total wax, or for a single MULTI-WICK candle
// whose wicks share one reservoir — not for two independent candles' lifetimes.
//
// This script recomputes, offline and dependency-free, every headline number the
// page displays, and asserts each against its target. It prints "N/N checks
// passed" and exits 1 if any check fails.
//
//   run:  node research/do-two-candles-burn-faster/verify-do-two-candles-burn-faster.mjs

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

// ── Sourced constants ───────────────────────────────────────────────────────
const BURN_RATE_G_MIN = 0.1;            // Wikipedia "Candle": ~0.1 g/min steady burn
const G_PER_HR        = BURN_RATE_G_MIN * 60;          // = 6 g/hr
const M_CH2           = 14.027;         // molar mass of a CH2 unit (paraffin ≈ (CH2)n), g/mol
const O2_PER_CH2      = 1.5;            // CH2 + 1.5 O2 -> CO2 + H2O
const MOLAR_VOL_25C   = 24.47;          // ideal-gas molar volume at 25 C, 1 atm, L/mol
const O2_FRAC         = 0.209;          // ambient O2 mole/volume fraction of air
const EXT_O2_FRAC     = 0.16;           // candle flame self-extinguishes near ~16% O2
const HOC_KJ_G        = 43.8;           // NIST (Hamins & Bundy): paraffin heat of combustion, kJ/g
const HUGGETT_KJ_GO2  = 13.1;           // Huggett's oxygen-consumption constant, kJ per g O2
const M_O2            = 31.998;         // molar mass O2, g/mol
const ROOM_ACH        = 0.35;           // older ASHRAE 62 residential minimum (1989), air changes/hr

// ── Stoichiometry: O2 required per gram of wax ──────────────────────────────
const molO2PerG = O2_PER_CH2 / M_CH2;                  // mol O2 / g wax
const litO2PerG = molO2PerG * MOLAR_VOL_25C;           // L O2 / g wax at 25 C
console.log('\n— paraffin stoichiometry (CH2 + 1.5 O2 -> CO2 + H2O) —');
check('O2 per gram wax ≈ 0.107 mol/g', approx(molO2PerG, 0.107, 0.001), `${molO2PerG.toFixed(4)} mol/g`);
check('O2 per gram wax ≈ 2.62 L/g (25 C)', approx(litO2PerG, 2.62, 0.01), `${litO2PerG.toFixed(3)} L/g`);

// cross-check via NIST heat of combustion / Huggett's constant
const gO2PerG_energy   = HOC_KJ_G / HUGGETT_KJ_GO2;    // g O2 / g wax
const molO2PerG_energy = gO2PerG_energy / M_O2;        // mol O2 / g wax
const relStoich = Math.abs(molO2PerG_energy - molO2PerG) / molO2PerG;
console.log('\n— cross-check: energy route (43.8 kJ/g ÷ 13.1 kJ/g-O2) —');
check('energy route gives ≈ 3.34 g O2/g wax', approx(gO2PerG_energy, 3.34, 0.02), `${gO2PerG_energy.toFixed(3)} g/g`);
check('stoichiometry & energy routes agree within 2.5%', relStoich < 0.025, `Δ=${(100 * relStoich).toFixed(1)}%`);

// ── Burn rate & heat release ────────────────────────────────────────────────
console.log('\n— burn rate & heat release —');
check('0.1 g/min = 6 g/hr', approx(G_PER_HR, 6, 1e-9), `${G_PER_HR} g/hr`);
const heatW = (G_PER_HR * HOC_KJ_G) / 3.6;             // kJ/hr -> W  (÷3600 s ×1000)
check('heat release ≈ 73 W (Wikipedia quotes ~80 W)', approx(heatW, 73, 1), `${heatW.toFixed(1)} W`);

// ── Per-candle O2 consumption ───────────────────────────────────────────────
const perCandleO2 = G_PER_HR * litO2PerG;              // L O2 / hr
console.log('\n— oxygen consumption —');
check('one candle uses ≈ 15.7 L O2/hr', approx(perCandleO2, 15.7, 0.1), `${perCandleO2.toFixed(2)} L/hr`);

// ── Room air & oxygen (default 4 x 4 x 2.5 m = 40 m3) ───────────────────────
const V_M3   = 4 * 4 * 2.5;
const V_L    = V_M3 * 1000;
const O2roomL = V_L * O2_FRAC;
console.log('\n— room air & oxygen (40 m3) —');
check('room = 40 m3 = 40,000 L air', approx(V_L, 40000, 1e-6), `${V_L} L`);
check('room holds ≈ 8,360 L O2', approx(O2roomL, 8360, 1), `${O2roomL} L`);

// ── Sealed-room self-extinction time (to ~16% O2) ───────────────────────────
const removableO2 = V_L * (O2_FRAC - EXT_O2_FRAC);     // L O2 before extinction
const sealed1 = removableO2 / perCandleO2;             // hr, 1 candle
const sealed2 = removableO2 / (2 * perCandleO2);       // hr, 2 candles
console.log('\n— sealed-room self-extinction (fixed-volume approximation) —');
check('removable O2 before ~16% = 1,960 L', approx(removableO2, 1960, 1), `${removableO2} L`);
check('sealed extinction ≈ 125 hr (1 candle)', approx(sealed1, 125, 1), `${sealed1.toFixed(1)} hr`);
check('sealed extinction ≈ 62 hr (2 candles)', approx(sealed2, 62, 1), `${sealed2.toFixed(1)} hr`);

// Debunking the "used up all the oxygen" myth: at the ~16% extinction point most
// of the original oxygen is STILL present. remaining = 16% / 20.9% ≈ 0.766.
const o2RemainingFrac = EXT_O2_FRAC / O2_FRAC;
check('at extinction ~77% of original O2 remains (only ~23% consumed)',
      approx(o2RemainingFrac, 0.766, 0.005),
      `${(100 * o2RemainingFrac).toFixed(1)}% present, ${(100 * (1 - o2RemainingFrac)).toFixed(1)}% consumed`);

// ── Ventilated room: supply vs consumption, steady-state O2 ─────────────────
const airflow   = ROOM_ACH * V_L;                      // L air / hr replaced
const ventO2    = airflow * O2_FRAC;                   // L O2 / hr delivered
const ratio1    = ventO2 / perCandleO2;
const ratio2    = ventO2 / (2 * perCandleO2);
const ss1       = O2_FRAC - perCandleO2 / airflow;     // steady-state O2 fraction, 1 candle
const ss2       = O2_FRAC - (2 * perCandleO2) / airflow;
console.log('\n— ventilated room (0.35 ACH) —');
check('fresh air delivers ≈ 2,926 L O2/hr', approx(ventO2, 2926, 1), `${ventO2.toFixed(0)} L/hr`);
check('supply/consumption ≈ 186x (1 candle)', approx(ratio1, 186, 1), `${ratio1.toFixed(1)}x`);
check('supply/consumption ≈ 93x (2 candles)', approx(ratio2, 93, 1), `${ratio2.toFixed(1)}x`);
check('steady-state O2 ≈ 20.8% (1 candle)', approx(ss1 * 100, 20.79, 0.05), `${(ss1 * 100).toFixed(2)}%`);
check('steady-state O2 ≈ 20.7% (2 candles)', approx(ss2 * 100, 20.68, 0.05), `${(ss2 * 100).toFixed(2)}%`);
check('O2 depression stays within ~0.2 pp of ambient (2 candles)',
      (O2_FRAC * 100 - ss2 * 100) < 0.25, `Δ=${(O2_FRAC * 100 - ss2 * 100).toFixed(2)} pp`);

// ── The fuel distinction: separate candles vs one multi-wick candle ─────────
// Per-candle lifetime = wax reservoir / burn rate. For SEPARATE candles this is
// independent of how many are lit. For a SINGLE candle with N wicks sharing one
// reservoir, the reservoir empties N times faster.
const WAX_G = 70;                                      // illustrative taper wax mass (free choice)
const lifeSeparate = WAX_G / G_PER_HR;                 // hr, per candle, any count
const lifeOneCandle = WAX_G / G_PER_HR;
const lifeTwoWick   = WAX_G / (2 * G_PER_HR);          // one shared reservoir, two wicks
console.log('\n— the fuel distinction (70 g reservoir, illustrative) —');
check('separate candles: each lasts the same whether 1 or 2 are lit',
      approx(lifeSeparate, lifeOneCandle, 1e-9), `${lifeSeparate.toFixed(2)} hr each`);
check('two wicks on ONE reservoir last exactly half as long',
      approx(lifeTwoWick, lifeSeparate / 2, 1e-9), `${lifeTwoWick.toFixed(2)} hr`);
check('total wax burned DOES double with a second separate candle',
      approx(2 * G_PER_HR, 12, 1e-9), `${2 * G_PER_HR} g/hr total`);
check('sealed-room extinction time far exceeds a candle lifetime (1 candle)',
      sealed1 > 5 * lifeSeparate, `${sealed1.toFixed(0)} hr vs ${lifeSeparate.toFixed(1)} hr`);

// ── Result ──────────────────────────────────────────────────────────────────
console.log(`\n${passed}/${total} checks passed`);
if (passed !== total) { console.log(`${total - passed} CHECK(S) FAILED`); process.exit(1); }
process.exit(0);
