#!/usr/bin/env node
// Verifier for the stratum "The Real Area of Contact" (adhesion science).
// Recomputes every number the page displays, from primary-source physics.
// Run: node research/real-area-of-contact/verify.mjs
//
// The spine: surfaces stick when they truly touch; real surfaces barely touch;
// every adhesion trick (glue, gecko, cold-weld) is a way of winning back true
// contact area. Each block below prints the claim, the arithmetic, and PASS/FAIL
// against the figure quoted on the page.

'use strict';

let pass = 0, fail = 0;
const results = [];
function check(name, got, want, tol, unit = '') {
  const rel = Math.abs(got - want) / (Math.abs(want) || 1);
  const ok = rel <= tol;
  results.push({ name, got, want, unit, ok, rel });
  if (ok) pass++; else fail++;
  const g = typeof got === 'number' ? got.toPrecision(4) : got;
  const w = typeof want === 'number' ? want.toPrecision(4) : want;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${name}\n        got ${g}${unit}  want ${w}${unit}  (rel ${(rel * 100).toFixed(2)}%)`);
}
function note(s) { console.log('        · ' + s); }
function head(s) { console.log('\n=== ' + s + ' ==='); }

// ─────────────────────────────────────────────────────────────────────────────
head('1. Van der Waals — the universal stickiness');
// Non-retarded vdW pressure between two flat parallel surfaces (Israelachvili,
// Intermolecular and Surface Forces, 3rd ed., ch. 13):
//   P(D) = A_H / (6 π D³)          [attractive, per unit area]
// Interaction energy per area at separation D:
//   W(D) = A_H / (12 π D²)
// At the universal contact separation D0 ≈ 0.165 nm this gives the work of
// adhesion of a van der Waals solid.
const A_H = 1.0e-19;        // Hamaker constant, J — representative (range ~0.4–4 ×10⁻¹⁹ J)
const D0  = 0.165e-9;       // Israelachvili universal cutoff, m
const W_vdw = A_H / (12 * Math.PI * D0 * D0);      // J/m²
console.log(`W_vdW = A_H/(12π·D0²) with A_H=${A_H} J, D0=${D0*1e9} nm`);
check('work of adhesion (vdW solid)', W_vdw * 1e3, 97.4, 0.02, ' mJ/m²');
note('~100 mJ/m² is the right order for a van der Waals solid (Israelachvili Table 13.3).');

// The 1/D³ collapse: bring surfaces from oxide-film separation (~1 nm) to true
// atomic contact (D0) and the pressure jumps by (1nm/0.165nm)³.
const jump = Math.pow(1e-9 / D0, 3);
check('pressure jump, 1 nm → 0.165 nm contact', jump, 222.6, 0.02, '×');
note('Why a book does not weld to a table: its "contact" is really ~1 nm of');
note('oxide + adsorbed grime away, and the 1/D³ law makes that gap decisive.');

// ─────────────────────────────────────────────────────────────────────────────
head('2. The real area of contact — Bowden & Tabor');
// Bowden & Tabor, The Friction and Lubrication of Solids (1950): asperities
// deform plastically until the true contact area supports the load:
//   A_real = L / H          (H = penetration hardness ≈ 3 × yield stress)
// This is INDEPENDENT of apparent area — the origin of Amontons' laws.
const H_steel = 2.0e9;      // penetration hardness of mild steel, Pa (representative ~1–3 GPa)
const load = 10;            // N  (≈ a 1 kg weight)
const A_real = load / H_steel;             // m²
const A_apparent = 1e-4;   // 1 cm² pressed flat, m²
const ratio = A_real / A_apparent;
console.log(`A_real = L/H = ${load} N / ${H_steel} Pa`);
check('true contact area (10 N on steel)', A_real * 1e6, 0.005, 0.001, ' mm²');
check('true / apparent contact fraction', ratio * 100, 0.005, 0.001, ' %');
note('A 1 kg block on a 1 cm² face touches at ~0.005 % of the face it looks flat against.');
// Real contact scales with LOAD, not apparent area — press twice as hard,
// touch twice as much true area, get twice the friction (Amontons).
const A_real_2x = (2 * load) / H_steel;
check('double the load → double true area', A_real_2x / A_real, 2.0, 1e-9, '×');

// ─────────────────────────────────────────────────────────────────────────────
head('3. The gecko — van der Waals, brute-forced by area (Autumn et al.)');
// Autumn, Liang, Hsieh, Zesch, Chan, Kenny, Fearing, Full, Nature 405:681 (2000),
// "Adhesive force of a single gecko foot-hair." MEASURED: a single seta gave up to
//   194 ± 25 µN (max, under perpendicular preload + a ~5 µm parallel drag) —
//   ~10× the 20 µN predicted from whole-animal data. ~500,000 setae per FOOT,
//   ~1 billion spatulae per foot. The paper's own whole-structure extrapolation:
//   one FOOT, all setae maximal → ~100 N. Mechanism nailed as van der Waals in
//   Autumn et al. PNAS 99:12252 (2002); Huber et al. PNAS 102:16293 (2005) showed a
//   secondary humidity/capillary contribution — so "dominantly", not "purely", vdW.
const F_seta_measured = 194e-6;   // N — measured max, Autumn 2000 (194 ± 25 SD)
const F_seta_foot = 100;          // N per foot, Autumn 2000 extrapolation (all setae maximal)
const N_setae_foot = 5e5;         // ~500,000 setae per foot, Autumn 2000
check('per-foot force ÷ setae ≈ measured max/seta (order)', F_seta_foot / N_setae_foot * 1e6, 200, 0.05, ' µN');
note('100 N/foot ÷ 500,000 setae ≈ 200 µN/seta — self-consistent with the ~194 µN measured.');

// The famous "could hold ~130 kg" figure: NOT from the 2000 Nature paper. It is
// Autumn & Peattie, Integ. Comp. Biol. 42:1081 (2002) / Autumn (2006):
//   6.5 million setae (whole animal) × ~200 µN → ~1300 N ≈ 130 kgf.
const F_seta_round = 200e-6;      // rounded value used in the 2002/2006 extrapolation
const N_setae_animal = 6.5e6;     // whole-animal setae, Autumn & Peattie 2002
const F_gecko_all = F_seta_round * N_setae_animal;   // N, ceiling
const g = 9.80665;
const mass_supported = F_gecko_all / g;              // kg
console.log(`ceiling = ${F_seta_round*1e6} µN/seta × ${N_setae_animal.toExponential(1)} setae (Autumn & Peattie 2002)`);
check('whole-animal force ceiling', F_gecko_all, 1300, 0.01, ' N');
check('mass that force could hold', mass_supported, 132.6, 0.01, ' kg');
note('HONEST: ~200 µN is the MAXIMUM per seta under ideal preload+drag; a walking');
note('gecko never engages all 6.5 M maximally. The 1300 N / ~130 kg is a CEILING');
note('(Autumn & Peattie 2002), not a load a gecko carries — the mechanism has that');
note('much headroom to spare. Body mass of study animals is ~50–70 g (uncertain).');

// ─────────────────────────────────────────────────────────────────────────────
head('4. Contact splitting — why divide a pad into a million hairs (Arzt 2003)');
// Arzt, Gorb, Spolenak, PNAS 100:10603 (2003), "From micro to nano contacts in
// biological attachment devices." For a pad of FIXED total area split into n
// self-similar contacts, JKR pull-off force scales as √n: each sub-contact keeps
// a larger share of its peel-line, so finer = stickier.
function splitForce(n) { return Math.sqrt(n); }   // total force relative to n=1, fixed pad area
check('split into 100 contacts → force ×', splitForce(100), 10, 1e-9, '×');
check('split into 1,000,000 contacts → force ×', splitForce(1e6), 1000, 1e-9, '×');
note('Bigger climbers carry FINER, DENSER terminal contacts — the cross-species');
note('trend Arzt et al. report (density rises with body mass). Splitting is how a');
note('gecko beats a fly with the same universal van der Waals force per real area.');

// ─────────────────────────────────────────────────────────────────────────────
head('5. JKR pull-off — why soft and tacky wins (Johnson–Kendall–Roberts 1971)');
// JKR, Proc. R. Soc. Lond. A 324:301 (1971). Pull-off force of an elastic sphere
// (radius R) from a flat: F = (3/2) π W R — INDEPENDENT of elastic modulus.
// (DMT limit, stiff/small: F = 2 π W R.) Softness doesn't change pull-off directly
// — it lets the solid FLOW into true contact, raising the effective area/W.
function jkrPulloff(W, R) { return 1.5 * Math.PI * W * R; }   // N
const W_tacky = 0.05;      // 50 mJ/m², a modest tacky work of adhesion
const R_ball = 5e-3;       // 5 mm sphere
const F_jkr = jkrPulloff(W_tacky, R_ball);
console.log(`F = 1.5π·W·R with W=${W_tacky} J/m², R=${R_ball*1e3} mm`);
check('JKR pull-off (5 mm tacky sphere)', F_jkr * 1e3, 1.178, 0.01, ' mN');
note('Coefficient 3/2 (JKR) vs 2 (DMT); ratio 0.75. Independent of stiffness —');
note('proof that adhesion is a surface-energy game, not a "how hard you press" game.');
check('JKR/DMT coefficient ratio', 1.5 / 2.0, 0.75, 1e-9, '');

// ─────────────────────────────────────────────────────────────────────────────
head('SUMMARY');
console.log(`\n${pass} passed, ${fail} failed, ${pass + fail} checks total.`);
process.exit(fail === 0 ? 0 : 1);
