#!/usr/bin/env node
// Verifier for the stratum "The Colour of Gold" (relativistic effects on chemistry)
//   page: public/strata/the-colour-of-gold/index.html
//   live: https://artwaste.land/strata/the-colour-of-gold/
//
// The page makes two kinds of claim, and this verifier keeps them apart on
// purpose — that separation IS the honesty bar of the piece:
//
//   (A) COMPUTED HERE, from first principles, with the SAME closed-form
//       expressions the page's dial uses live: the exact Dirac one-electron
//       (point-nucleus) 1s binding energy, the 1s electron speed v/c = Zα, the
//       Lorentz factor γ, the orbital-radius contraction 1/γ, and the
//       photon energy <-> wavelength bridge. These are checked against known
//       limits and textbook numbers.
//
//   (B) REPORTED from the cited literature and NOT reproducible by a hydrogenic
//       toy: gold's ~2.4 eV interband (5d->6s) absorption edge, silver's ~3.7 eV
//       edge, the gold 6s radial contraction (~18%), and mercury's melting point
//       with vs. without relativity. The verifier records these as fixed
//       constants with their sources and checks only that the page quotes them
//       consistently (the arithmetic AROUND them — wavelengths, colour band — is
//       still checked), never that the toy model derives them. It cannot, and
//       the page says so.
//
// Run:  node research/relativistic-gold/verify.mjs
// Exit: 0 if every check passes, 1 otherwise. No dependencies (Node 18+).

let pass = 0, fail = 0;
function check(label, cond, got) {
  if (cond) { pass++; console.log(`  ok   ${label}`); }
  else { fail++; console.log(`  FAIL ${label}` + (got !== undefined ? `  (got: ${got})` : "")); }
}
const approx = (a, b, tol) => Math.abs(a - b) <= tol;
const rel = (a, b) => Math.abs(a - b) / Math.abs(b); // relative error

// ---------------------------------------------------------------------------
// Physical constants (CODATA 2018, the values the page also uses)
// ---------------------------------------------------------------------------
const MC2_eV   = 510998.95;          // electron rest energy m_e c^2, in eV
const ALPHA    = 1 / 137.035999084;  // fine-structure constant
const HC_eVnm  = 1239.841984;        // h c, in eV*nm  (E[eV] = hc/λ[nm])
const RY_eV    = 13.605693;          // Rydberg energy (1s binding of hydrogen, nonrel)

// ---------------------------------------------------------------------------
// (A) The closed forms the page computes live.  One electron, point nucleus.
// ---------------------------------------------------------------------------

// Exact Dirac binding energy of the 1s_{1/2} level (positive = how deep below
// the continuum), for nuclear charge Z.  Derivation: the Sommerfeld/Dirac level
//   E_{n,j} = mc^2 [ (1 + (Zα / (n - δ_j + sqrt((j+1/2)^2 - (Zα)^2)))^2)^{-1/2} - 1 ]
// collapses, for the ground state n=1, j=1/2, to the clean form below.
function diracBind1s(Z) {
  const za = Z * ALPHA;
  return MC2_eV * (1 - Math.sqrt(1 - za * za)); // binding energy (eV), positive
}
// Nonrelativistic (Schrödinger/Bohr) 1s binding: Z^2 * Rydberg = (1/2) mc^2 (Zα)^2
function nonrelBind1s(Z) {
  const za = Z * ALPHA;
  return 0.5 * MC2_eV * za * za;
}
// Bohr speed of the 1s electron, as a fraction of c
const vOverC = (Z) => Z * ALPHA;
// Lorentz factor of that electron
const gamma  = (Z) => 1 / Math.sqrt(1 - vOverC(Z) ** 2);
// Relativistic radius contraction of the 1s orbit: a ∝ 1/(m v) ⇒ shrinks by 1/γ
const contraction = (Z) => 1 / gamma(Z);
// photon bridge
const eVtoNm = (eV) => HC_eVnm / eV;

console.log("\n(A) Computed live by the page's dial — checked here\n");

// --- limit check: hydrogen (Z=1) reduces to the Rydberg, with a tiny shift ---
{
  const nr = nonrelBind1s(1);
  check("Z=1 nonrel 1s binding == Rydberg 13.606 eV", approx(nr, RY_eV, 0.002), nr.toFixed(4));
  const d = diracBind1s(1);
  // Dirac adds a positive O((Zα)^4) correction; for H it is ~1.8e-4 eV, deeper.
  check("Z=1 Dirac binding exceeds nonrel by a tiny relativistic correction",
        d > nr && (d - nr) < 1e-3, `Δ=${((d - nr) * 1e3).toFixed(4)} meV`);
}

// --- the series expansion: Dirac binding -> nonrel + (3/8)mc^2 (Zα)^4 + ... ---
// 1 - sqrt(1-x) = x/2 + x^2/8 + ...  with x=(Zα)^2 gives leading nonrel term x/2,
// first correction (Zα)^4/8 * mc^2.  Check the small-Z agreement.
{
  const Z = 10, za2 = (Z * ALPHA) ** 2;
  const lead = 0.5 * MC2_eV * za2;
  const corr = (1 / 8) * MC2_eV * za2 * za2;
  const d = diracBind1s(Z);
  check("Dirac 1s ≈ nonrel + (1/8)mc^2(Zα)^4 for Z=10",
        rel(d, lead + corr) < 1e-4, `d=${d.toFixed(3)}  approx=${(lead + corr).toFixed(3)}`);
}

// --- GOLD, Z=79: the headline numbers on the dial -------------------------
{
  const Z = 79;
  const v = vOverC(Z);
  check("gold 1s speed v/c = Zα ≈ 0.5765", approx(v, 0.5765, 0.0005), v.toFixed(4));
  const g = gamma(Z);
  check("gold 1s Lorentz factor γ ≈ 1.224", approx(g, 1.224, 0.001), g.toFixed(4));
  const c = contraction(Z);
  check("gold 1s radius contracts to ≈ 0.817 of nonrel (≈18% smaller)",
        approx(c, 0.8171, 0.001), c.toFixed(4));
  const d = diracBind1s(Z), nr = nonrelBind1s(Z);
  check("gold Dirac 1s binding ≈ 93.5 keV (point nucleus)", approx(d, 93460, 200), d.toFixed(0));
  check("gold nonrel 1s binding ≈ 84.9 keV", approx(nr, 84911, 200), nr.toFixed(0));
  check("relativity deepens gold's 1s by ≈ 10% (ratio ≈ 1.10)",
        approx(d / nr, 1.101, 0.003), (d / nr).toFixed(4));
}

// --- the crossover: where does the 1s electron pass half light-speed? -------
// v/c = Zα = 1/2  ⇒  Z = 1/(2α) = 68.5.  So every element past ytterbium-ish has
// a core electron moving faster than half c.
{
  const Zhalf = 0.5 / ALPHA;
  check("1s electron reaches v=c/2 at Z ≈ 68.5", approx(Zhalf, 68.5, 0.1), Zhalf.toFixed(2));
  check("→ gold (79), mercury (80), lead (82), uranium (92) are all past it",
        79 > Zhalf && 92 > Zhalf, `Z½=${Zhalf.toFixed(1)}`);
}

// --- the formula's own edge, named honestly: Zα→1 at Z≈137 ------------------
// At Zα=1 the point-nucleus Dirac 1s binding reaches mc^2 (the model's collapse;
// the real story past ~Z137 needs finite nuclear size + QED). The page states
// this limit; we check the toy hits it.
{
  const Zc = 1 / ALPHA;
  check("toy Dirac 1s binding → mc^2 as Z → 1/α ≈ 137.0",
        approx(Zc, 137.04, 0.05) && approx(diracBind1s(137), MC2_eV * (1 - Math.sqrt(1 - (137*ALPHA)**2)), 1),
        `Zc=${Zc.toFixed(2)}`);
}

// --- photon bridge, used for every colour claim -----------------------------
{
  check("E↔λ bridge: 2.0 eV ↔ 620 nm (red)", approx(eVtoNm(2.0), 619.9, 0.5), eVtoNm(2.0).toFixed(1));
  check("E↔λ bridge: 3.1 eV ↔ 400 nm (violet edge)", approx(eVtoNm(3.1), 400.0, 0.5), eVtoNm(3.1).toFixed(1));
}

// ---------------------------------------------------------------------------
// (B) Reported from the literature.  Fixed constants with sources.
//     The verifier checks the page's ARITHMETIC around them, never that the
//     hydrogenic toy reproduces them (it cannot, and the page says so).
// ---------------------------------------------------------------------------
console.log("\n(B) Reported from cited full-relativistic work — arithmetic checked, model NOT claimed\n");

// Gold's interband (5d -> 6s/Fermi) absorption onset. Experiment + relativistic
// band structure put it near 2.4 eV; nonrelativistic band structure pushes it up
// toward ~3.5 eV (gold would look silvery). Sources: Pyykkö & Desclaux,
// Acc. Chem. Res. 12 (1979) 276; Pyykkö, Chem. Rev. 88 (1988) 563; Glantschnig &
// Ambrosch-Draxl, New J. Phys. 12 (2010) 103048 (rel. vs nonrel. optical spectra).
const AU_EDGE_eV    = 2.4;   // relativistic / experimental
const AU_EDGE_NR_eV = 3.5;   // nonrelativistic band structure (≈ silver-like)
const AG_EDGE_eV    = 3.9;   // silver interband onset (UV); broad visible reflectivity gives a white appearance

{
  const lam = eVtoNm(AU_EDGE_eV);
  check("gold's 2.4 eV edge sits at ≈ 517 nm, in green (495-570 nm)",
        approx(lam, 516.6, 1) && lam >= 495 && lam <= 570, lam.toFixed(1) + " nm");
  const lamAg = eVtoNm(AG_EDGE_eV);
  check("silver's 3.9 eV interband edge sits at ≈ 318 nm, in the ultraviolet",
        approx(lamAg, 317.9, 1) && lamAg < 380, lamAg.toFixed(1) + " nm");
  check("relativity drops gold's interband edge from ultraviolet (~3.5 eV) into green (~2.4 eV)",
        eVtoNm(AU_EDGE_NR_eV) < 380 && eVtoNm(AU_EDGE_eV) >= 495 && eVtoNm(AU_EDGE_eV) <= 570,
        `${AU_EDGE_NR_eV} → ${AU_EDGE_eV} eV`);
  check("silver's edge stays in the UV whether or not relativity is on (4d-5s gap already wide) → silver is white either way",
        AG_EDGE_eV > 3.5, `${AG_EDGE_eV} eV`);
}

// Gold 6s radial contraction from full relativistic atomic calculation (the
// chemically operative one — distinct from the 1s 1/γ above, though similar in
// size by coincidence). Pyykkö & Desclaux 1979 / Pyykkö 1988: <r>_6s drops ~18%.
const AU_6s_CONTRACTION = 0.18;
{
  // sanity: it is the same order as the computed 1s 1/γ shift, but they are
  // different orbitals — the page must not conflate them.
  const oneS = 1 - contraction(79);
  check("cited gold 6s contraction (~18%) is same ORDER as computed 1s shift (~18%) but a DIFFERENT orbital",
        approx(AU_6s_CONTRACTION, 0.18, 0.001) && Math.abs(AU_6s_CONTRACTION - oneS) < 0.05,
        `6s≈${AU_6s_CONTRACTION}, 1s 1-1/γ≈${oneS.toFixed(3)}`);
}

// Mercury's melting point, with and without relativity. Calvo, Pahl, Wormit,
// Schwerdtfeger, Angew. Chem. Int. Ed. 52 (2013) 7583: relativistic ab initio
// gives Tm ≈ 250 K; switching relativity off gives Tm ≈ 355 K; experiment 234 K.
// Without relativity mercury would be SOLID at room temperature (293–298 K).
const HG_TM_REL_K = 250, HG_TM_NONREL_K = 355, HG_TM_EXP_K = 234.32, ROOM_K = 295;
{
  check("relativity lowers Hg melting point by ≈105 K (355 → 250 K, Calvo et al. 2013)",
        approx(HG_TM_NONREL_K - HG_TM_REL_K, 105, 1), `${HG_TM_NONREL_K}-${HG_TM_REL_K}`);
  check("without relativity Hg would be SOLID at room temperature (355 K > 295 K)",
        HG_TM_NONREL_K > ROOM_K, `${HG_TM_NONREL_K} K`);
  check("with relativity Hg is liquid at room temperature (250 K, exp 234 K, both < 295 K)",
        HG_TM_REL_K < ROOM_K && HG_TM_EXP_K < ROOM_K, `${HG_TM_REL_K}/${HG_TM_EXP_K} K`);
  check("experimental Hg melting point −38.83 °C == 234.32 K", approx(HG_TM_EXP_K - 273.15, -38.83, 0.01),
        (HG_TM_EXP_K - 273.15).toFixed(2) + " °C");
}

// ---------------------------------------------------------------------------
console.log(`\n${fail === 0 ? "PASS" : "FAIL"} — ${pass}/${pass + fail} checks\n`);
process.exit(fail === 0 ? 0 : 1);
