#!/usr/bin/env node
// verify.mjs — the chimney stack effect, computed from first principles.
//
// Claim of the stratum "The Weight That Lifts the Smoke" (/strata/chimney-stack-effect/):
// a chimney does not draw because "hot air rises". It draws because the column of cold,
// dense air OUTSIDE weighs more, per unit height, than the column of hot flue gas inside.
// That surplus weight — a pressure difference at the base — is the draft. It scales with
// chimney HEIGHT (linearly) and with the temperature difference, not with how eagerly hot
// air "wants" to rise. When the flue is no hotter than the outside air, the draft is zero.
//
// This script recomputes every number the page shows, so the page never merely asserts them.
//
// Run:  node research/chimney-stack-effect/verify.mjs

const g   = 9.81;          // m/s^2
const P0  = 101325;        // Pa, standard sea-level pressure
const M   = 0.028964;      // kg/mol, dry-air molar mass
const R   = 8.314462618;   // J/(mol·K)
const RHO_WATER = 999.972; // kg/m^3, water near 4 °C (for the mm-of-water conversion)

// Ideal gas at fixed 1 atm:  rho = P·M/(R·T) = K/T
const K = (P0 * M) / R;                 // ~= 352.97  (the "353/T" rule of thumb)
const rho = (T_kelvin) => K / T_kelvin; // air density, kg/m^3, at 1 atm

// Theoretical (frictionless) stack draft: the weight difference of two equal-height columns.
function draft(h_m, To_C, Ti_C) {
  const To = To_C + 273.15, Ti = Ti_C + 273.15;
  const rho_o = rho(To), rho_i = rho(Ti);
  const dP = h_m * g * (rho_o - rho_i);           // Pa  (>0 when Ti>To → draws)
  const v  = dP > 0 ? Math.sqrt(2 * dP / rho_i) : 0; // m/s, frictionless UPPER BOUND
  return { rho_o, rho_i, dP, v };
}

const Pa_to_mmH2O = (Pa) => (Pa / (RHO_WATER * g)) * 1000;
const inchWC_to_Pa = RHO_WATER * g * 0.0254;

let pass = 0, fail = 0;
const approx = (a, b, tol, label) => {
  const ok = Math.abs(a - b) <= tol;
  console.log(`  ${ok ? 'PASS' : 'FAIL'}  ${label}: got ${a.toFixed(4)}, expected ${b} (±${tol})`);
  ok ? pass++ : fail++;
};

console.log('\n[1] Air-density constant  rho = K/T');
approx(K, 352.97, 0.05, 'K = P0·M/R');

console.log('\n[2] Worked example A — h=5 m, outside 10 °C, flue 70 °C (a modest warm flue)');
const A = draft(5, 10, 70);
approx(A.rho_o, 1.2466, 0.001, 'rho_outside');
approx(A.rho_i, 1.0286, 0.001, 'rho_flue');
approx(A.dP, 10.69, 0.05, 'draft ΔP (Pa)');
approx(Pa_to_mmH2O(A.dP), 1.090, 0.01, 'draft (mm H2O)');
approx(A.v, 4.56, 0.02, 'frictionless velocity v (m/s, upper bound)');

console.log('\n[3] Worked example B — h=5 m, outside 0 °C, flue 200 °C (a hot flue)');
const B = draft(5, 0, 200);
approx(B.dP, 26.79, 0.05, 'draft ΔP (Pa)');

console.log('\n[4] Height is LINEAR — double the chimney, double the draft (same temps)');
const A10 = draft(10, 10, 70);
approx(A10.dP, 2 * A.dP, 1e-9, 'ΔP(10 m) = 2·ΔP(5 m)');

console.log('\n[5] No temperature difference → no draft (a cold chimney does not draw)');
approx(draft(5, 10, 10).dP, 0, 1e-9, 'ΔP when flue = outside temp');

console.log('\n[6] Reversal — a flue COLDER than outside air pushes the wrong way (backdraft)');
const rev = draft(5, 20, 5); // warm house air sitting in a flue, colder outside... modelled as Ti<To
approx(Math.sign(rev.dP), -1, 1e-9, 'ΔP < 0 when flue is colder than outside');

console.log('\n[7] Equivalence to the standard engineering formula ΔP = C·a·h·(1/To − 1/Ti)');
// The chimney-draft literature quotes C ≈ 0.0342 with a = atmospheric pressure (Pa).
// That constant IS g·K in disguise:
approx(g * K, 0.0342 * P0, 4, 'g·K  vs  0.0342·P0');
// Cross-check example A through the engineering form:
const To = 283.15, Ti = 343.15;
const dP_eng = 0.0342 * P0 * 5 * (1 / To - 1 / Ti);
approx(dP_eng, A.dP, 0.05, 'engineering-formula ΔP matches column-weight ΔP');

console.log('\n[8] Unit conversions the page quotes');
approx(Pa_to_mmH2O(1), 0.10194, 0.0002, '1 Pa in mm of water');
approx(inchWC_to_Pa, 249.17, 0.2, '1 inch of water column in Pa');

console.log(`\n${fail === 0 ? '✓ ALL CHECKS PASS' : '✗ ' + fail + ' FAILED'} (${pass} passed, ${fail} failed)\n`);

// --- Honest apparatus (assumptions the page also names) ---
// • Ideal gas at a fixed 1 atm; density taken as K/T. Real barometric pressure shifts every
//   density by the same factor, so ΔP scales with local pressure but the RATIOS are unchanged.
// • Flue gas is modelled as hot AIR. Real combustion gas has a slightly different molar mass and
//   carries water vapour; the correction is a few percent and does not change the mechanism.
// • Each column is treated as ISOTHERMAL. A real flue cools with height, so the effective mean
//   flue temperature — and thus the real draft — is LOWER than this idealised maximum.
// • v = sqrt(2ΔP/rho_i) is the FRICTIONLESS ceiling. Wall friction and flow resistance mean the
//   actual gas velocity is well below it (typically ~1–3 m/s); ΔP is spent driving flow, not all
//   converted to speed. The page states v as an upper bound, never as the real exit speed.

process.exit(fail === 0 ? 0 : 1);
