#!/usr/bin/env node
// verify.mjs: the listed model quantities and comparisons, recomputed
// from published inputs. This does not check the page's historical claims. Run:
//   node research/glass-is-liquid/verify.mjs
//
// The claim the page settles (narrow and sturdy): window glass is an AMORPHOUS
// SOLID at room temperature. Above Tg the hot-glass model gives a viscosity;
// crossing Tg alone does not make it pourable. Old cathedral panes are uneven
// because of manufacture, including crown and cylinder methods, not visible flow.
// Two cited room-temperature estimates give characteristic times of billions
// to trillions of years here. They are illustrative inputs, not bounds for all glass.
//
// Sources for the CITED (not computed here) facts:
//   Zanotto, E. D. "Do cathedral glasses flow?" Am. J. Phys. 66, 392 (1998).
//   Zanotto, E. D. & Gupta, P. K. "…Additional remarks." Am. J. Phys. 67, 260 (1999).
//   Gulbiten, O.; Mauro, J. C.; et al. "Viscous Flow of Medieval Cathedral Glass."
//     J. Am. Ceram. Soc. (online 2017; vol. 101, 2018). DOI 10.1111/jace.15092.
//     -> calculated maximum ~1 nm over one billion years; inferred viscosity
//        about 16 orders below a previous soda-lime study. The experimental sample
//        was newly made with a typical Westminster Abbey glass composition.
//   Scientific American, "Fact or Fiction?: Glass Is a (Supercooled) Liquid" (Ediger, Brill).
//   Lyon (1974), Appendix B, p. 503 (PMC6742813): Fulcher constants derived from
//     composition-predicted viscosities at 700, 900 and 1300 C.
//     https://nvlpubs.nist.gov/nistpubs/jres/78a/jresv78an4p497_a1b.pdf
//   Brill, Glass Flow?, p. 2: historical room-temperature estimate 10^20 poise.
//     https://www.glassnotes.com/resources/pdf/Glass-Flow.pdf
//   Physics FAQ, Types of Glass: room-temperature extrapolation 10^22 Pa s.
//     https://math.ucr.edu/home/baez/physics/General/Glass/glass.html
//
// The viscosity model (the one thing the page computes live):
//   Vogel-Fulcher-Tammann (VFT):  log10(eta_poise) = A + B/(T - T0),  T in deg C.
//   The page uses it only at or above ~Tg. Below its pole T0 it becomes
//   nonphysical; that model failure does not itself prove the absence of flow.
//   Room-temperature inputs are separately sourced estimates, not measurements.

let pass = 0, fail = 0;
const approx = (a, b, rel = 0.03) => Math.abs(a - b) <= Math.abs(b) * rel;
function check(name, got, want, rel) {
  const ok = approx(got, want, rel);
  const g = typeof got === 'number' ? got.toPrecision(5) : got;
  console.log(`${ok ? 'ok  ' : 'FAIL'}  ${name}: ${g}  (expect ≈ ${typeof want === 'number' ? want.toPrecision(5) : want})`);
  ok ? pass++ : fail++;
}
function assert(name, cond) {
  console.log(`${cond ? 'ok  ' : 'FAIL'}  ${name}`);
  cond ? pass++ : fail++;
}

// ── The published VFT fit (poise, deg C) ─────────────────────────────────────
const A = -1.594, B = 4111.7, T0 = 280.3;     // Lyon (1974), Appendix B
const logEtaPoise = (Tc) => A + B / (Tc - T0);
const etaPaS = (Tc) => Math.pow(10, logEtaPoise(Tc) - 1);  // 1 Pa·s = 10 poise
const Tat = (logPoise) => T0 + B / (logPoise - A);          // invert for fixed points

// 1 ── The fit reproduces the four standard glassblowers' fixed points.
//     (poise): working 10^4, softening 10^7.6, annealing 10^13, strain 10^14.5.
//     These are the canonical viscosity reference points; landing them at the
//     textbook temperatures is the cross-check that the constants are real.
check('working point  (10^4 poise) lands near 1000 C', Tat(4),    1015, 0.05);
check('softening pt  (10^7.6 poise) lands near 700 C', Tat(7.6),   728, 0.05);
check('annealing/Tg (10^13 poise) lands near 555 C',   Tat(13),    562, 0.05);
check('strain point (10^14.5 poise) lands near 515 C', Tat(14.5),  536, 0.05);

// 2 ── Tg as the conventional viscosity isopleth: eta(Tg) ≈ 10^12 Pa·s = 10^13 poise.
//     The page draws the "freezing line" at Tg ≈ 560 C; confirm the convention.
const Tg = 560;
check('eta at Tg≈560C ≈ 10^12 Pa·s (the Tg convention)', Math.log10(etaPaS(Tg)), 12, 0.06);
assert('room temp (20C) is hundreds of deg below Tg', Tg - 20 > 400);
assert('room temp is below the Vogel pole T0 — VFT is INVALID there', 20 < T0);

// 3 ── Flow / sag time:  characteristic tau = eta / (rho * g * h).
//     An order-of-magnitude relaxation time for a pane of thickness h to deform
//     appreciably under its own weight. NOT a precise sag prediction — it is the
//     standard back-of-envelope used to show the timescales, and the page says so.
const rho = 2500, g = 9.81, h = 0.003;        // kg/m^3, m/s^2, 3 mm pane
const denom = rho * g * h;                     // Pa  (≈ 73.6)
const tauSec = (eta) => eta / denom;
const SEC_PER_YR = 365.25 * 86400;             // 3.15576e7
const tauYr = (eta) => tauSec(eta) / SEC_PER_YR;
const AGE_UNIVERSE_YR = 1.38e10;

check('rho*g*h for a 3mm pane ≈ 73.6 Pa', denom, 73.575, 0.001);

// At the melt/working temperature a pane sags in ~seconds (it really is a liquid).
const tMelt = 1200;
check('sag time at 1200 C ≈ 1 second', tauSec(etaPaS(tMelt)), 1.02, 0.1);
// At Tg the same pane takes centuries.
check('sag time at Tg(560C) ≈ 550 years', tauYr(etaPaS(Tg)), 550, 0.15);
assert('sag time at 600 C is years, not seconds (≈ 8 yr)',
       tauYr(etaPaS(600)) > 1 && tauYr(etaPaS(600)) < 100);

// 4: Two cited estimates, not a measured interval or a bound for every composition.
//     Brill gives 10^20 poise; the Physics FAQ gives 10^22 Pa s as an extrapolation.
const brillEtaPaS = 1e20 / 10;
check('Brill: 10^20 poise = 10^19 Pa·s', brillEtaPaS, 1e19, 1e-12);
const tauYr_e19 = tauYr(brillEtaPaS);
const tauYr_e22 = tauYr(1e22);
console.log(`     room-temp model times: ${tauYr_e19.toExponential(2)} yr (η=1e19) … ${tauYr_e22.toExponential(2)} yr (η=1e22)`);
check('room-temp model time at Brill input ≈ 4.31 billion years', tauYr_e19, 4.31e9, 0.002);
check('room-temp model time at FAQ input ≈ 4.31 trillion years', tauYr_e22, 4.31e12, 0.002);
assert('Brill input gives > 1 billion years but < age of universe',
       tauYr_e19 > 1e9 && tauYr_e19 < AGE_UNIVERSE_YR);

// 5 ── Independent cross-check of the conclusion via the 2018 medieval-glass study:
//     ~1 nm of flow per 1e9 years. Over the ~800 years a medieval window has
//     stood, that is sub-atomic — utterly invisible. (Cited figure, sanity-checked.)
const flowNmPerGyr = 1.0;
const medievalWindowAgeYr = 800;
const flowOver800yr_nm = flowNmPerGyr * (medievalWindowAgeYr / 1e9);
console.log(`     medieval pane flow over ${medievalWindowAgeYr} yr ≈ ${flowOver800yr_nm.toExponential(2)} nm (≪ one atom)`);
assert('800-yr flow is far less than an atomic diameter (~0.3 nm)', flowOver800yr_nm < 0.3);
check('800-yr scaled maximum ≈ 2.7 millionths of a 0.3 nm diameter',
      flowOver800yr_nm / 0.3, 2.7e-6, 0.02);
check('1 mm exceeds the 800-yr scaled maximum by ≈ 12.1 orders',
      Math.log10(1e6 / flowOver800yr_nm), 12.1, 0.001);

// 6 ── Viscosity ladder shown on the page (Pa·s), for the comparison rungs.
//     water ~1e-3, honey ~1e1, glass at Tg ~1e12, room estimates ~1e19 and 1e22.
check('glass at Tg is ~11 orders of magnitude more viscous than honey',
      Math.log10(etaPaS(Tg)) - 1, 11, 0.12);   // log(1e12) - log(1e1) = 11
assert('the two room-temp inputs are ≥18 orders more viscous than honey',
       Math.log10(brillEtaPaS) - Math.log10(1e1) >= 18);

console.log(`\n${fail ? 'FAIL' : 'PASS'} — ${pass} ok, ${fail} failed`);
process.exit(fail ? 1 : 0);
