#!/usr/bin/env python3
"""
A Name Is Not a Thing -- companion script v1
solvetheuniverse.com / Pressure-Based Theory, 2026-09-24

WHAT THIS IS. The numbers behind the article, re-runnable by anyone. Three
short parts, each with its inputs named:

  A. The "5 / 26 / 69 percent" budget, from the Planck 2018 abstract values
     rather than from memory: Omega_b h^2, Omega_c h^2 and H0 give the
     baryon and cold-dark-matter fractions; the flat-universe closure gives
     the dark-energy fraction. These are the numbers the phrase "95 percent
     unknown" rests on, and this shows where each comes from.
  B. What a flat rotation curve says about the mass inside radius r. For a
     test body on a circular orbit, v^2 = G M(<r) / r, so v constant means
     M(<r) grows in proportion to r, and the density falls as 1/r^2. That
     inference uses Newtonian gravity and nothing about what the mass is.
     The contrast case, all the mass at the centre, gives v falling as
     r^(-1/2). Printed for a stated stellar mass and radius so the reader can
     see the size of the gap, not to model any particular galaxy.
  C. The DESI DR2 (2025) hint of evolving dark energy: with the two-parameter
     equation of state w(z) = w0 + wa z/(1+z) and the best-fit values quoted
     in the DESI summaries (w0 about -0.7, wa about -1), the redshift at which
     w crosses -1 follows in one line. The inputs are the survey's quoted
     best-fit values; the crossing redshift is arithmetic on them.

INPUTS:
  Planck 2018 results VI (arXiv:1807.06209, abstract): Omega_b h^2 = 0.0224
    +/- 0.0001, Omega_c h^2 = 0.120 +/- 0.001, H0 = 67.4 +/- 0.5 km/s/Mpc,
    Omega_m = 0.315 +/- 0.007. Read from the abstract 2026-09-24.
  DESI DR2 best-fit w0 ~ -0.7, wa ~ -1 for the DESI + CMB + Union3 combination
    (DESI DR2 results II, Phys. Rev. D 112, 083515 (2025), as summarised by the
    collaboration's March 2025 guide and the astrobites write-up; not read from
    the paper's own tables). Other combinations: Pantheon+ (-0.85, -0.54),
    DESY5 (-0.76, -0.77), DESI+CMB alone (-0.42, -1.75); crossings 0.39-0.50.
  G = 6.67430e-11, one solar mass = 1.98892e30 kg, one kpc = 3.0857e19 m.

Run:
  python3 a-name-is-not-a-thing-v1.py
  python3 a-name-is-not-a-thing-v1.py --selftest
"""
import argparse, math

G = 6.67430e-11
M_SUN = 1.98892e30
KPC = 3.0857e19

PLANCK = dict(omega_b_h2=0.0224, omega_c_h2=0.120, H0=67.4, omega_m=0.315)
DESI = dict(w0=-0.7, wa=-1.0)

# ---- A: the budget --------------------------------------------------------------
def fractions(omega_b_h2, omega_c_h2, H0):
    h = H0 / 100.0
    ob = omega_b_h2 / h**2
    oc = omega_c_h2 / h**2
    om = ob + oc
    ol = 1.0 - om                     # flat universe (Planck's baseline assumption)
    return dict(h=h, baryons=ob, cold_dark_matter=oc, matter=om, dark_energy=ol)

# ---- B: the rotation curve inference ---------------------------------------------
def enclosed_mass_for_flat_curve(v_kms, r_kpc):
    """M(<r) = v^2 r / G for a circular orbit at speed v and radius r."""
    v = v_kms * 1e3; r = r_kpc * KPC
    return v**2 * r / G

def keplerian_speed(M_kg, r_kpc):
    """v = sqrt(G M / r): the speed if all mass M sat inside r."""
    return math.sqrt(G * M_kg / (r_kpc * KPC)) / 1e3

# ---- C: the DESI crossing --------------------------------------------------------
def w_of_z(z, w0, wa):
    return w0 + wa * z / (1.0 + z)

def crossing_redshift(w0, wa):
    """Solve w(z) = -1: z/(1+z) = (-1 - w0)/wa  ->  z = x/(1-x).
    Returns None when there is no crossing (wa = 0, or the crossing would sit
    at z < 0 or at infinite redshift)."""
    if wa == 0.0:
        return None
    x = (-1.0 - w0) / wa
    if not (0.0 <= x < 1.0):
        return None
    return x / (1.0 - x)

# ---- report ----------------------------------------------------------------------
def run():
    print("=" * 74)
    print("A Name Is Not a Thing -- companion script v1 (2026-09-24)")
    print("=" * 74)
    f = fractions(PLANCK["omega_b_h2"], PLANCK["omega_c_h2"], PLANCK["H0"])
    print("\n[A] the budget, from Planck 2018's abstract values (h = %.4f):" % f["h"])
    for k in ("baryons", "cold_dark_matter", "matter", "dark_energy"):
        print(f"    {k:17s} {100*f[k]:5.1f} %")
    print(f"    Planck's own Omega_m = {PLANCK['omega_m']:.3f}; computed here {f['matter']:.3f}")
    print(f"    'unknown' share (cold dark matter + dark energy): {100*(f['cold_dark_matter']+f['dark_energy']):.1f} %")

    print("\n[B] what a flat rotation curve implies (Newtonian gravity, no assumption about what the mass is):")
    v, r = 220.0, 30.0
    M = enclosed_mass_for_flat_curve(v, r)
    print(f"    v = {v:.0f} km/s flat out to r = {r:.0f} kpc  ->  M(<r) = {M/M_SUN:.2e} solar masses")
    M_star = 6e10 * M_SUN
    print(f"    if the stars and gas were {M_star/M_SUN:.0e} solar masses all inside that radius, the speed there would be"
          f" {keplerian_speed(M_star, r):.0f} km/s and falling as r^(-1/2)")
    print(f"    flat curve => M(<r) proportional to r; for r doubling, M doubles: M(<60 kpc) = {enclosed_mass_for_flat_curve(v, 2*r)/M_SUN:.2e}")

    print("\n[C] DESI DR2's evolving-dark-energy hint, as arithmetic on its quoted best fit:")
    zc = crossing_redshift(DESI["w0"], DESI["wa"])
    print(f"    w(z) = w0 + wa z/(1+z) with w0 = {DESI['w0']}, wa = {DESI['wa']}")
    print(f"    w today = {w_of_z(0, **DESI):.2f}; w crosses -1 at z = {zc:.2f}; w at z = 2: {w_of_z(2, **DESI):.2f}")
    print("    (a cosmological constant is w = -1 at every z; the fit's significance is the survey's number, ~3 sigma, not this script's)")
    print("\nNotes: [A] is arithmetic on published parameters; [B] is Newton's law solved for the enclosed mass;"
          "\n[C] is arithmetic on a quoted fit (the DESI+CMB+Union3 combination). None of it says what dark matter or dark energy is.")

# ---- self-test -------------------------------------------------------------------
def selftest():
    n = 0
    f = fractions(PLANCK["omega_b_h2"], PLANCK["omega_c_h2"], PLANCK["H0"])
    # 1. cold dark matter outweighs baryons by the well-known factor ~5.4 (a real check;
    #    'fractions sum to 1' would be vacuous since the closure builds it in)
    assert 5.0 < f["cold_dark_matter"] / f["baryons"] < 5.8, f["cold_dark_matter"] / f["baryons"]; n += 1
    # 2. computed Omega_m matches Planck's quoted 0.315 within its 0.007
    assert abs(f["matter"] - PLANCK["omega_m"]) < 0.007, f["matter"]; n += 1
    # 3. the familiar rounded budget: 5 / 26-27 / 68-69 percent
    assert 4.5 < 100*f["baryons"] < 5.5 and 25 < 100*f["cold_dark_matter"] < 28 and 67 < 100*f["dark_energy"] < 70; n += 1
    # 4. flat curve: M(<2r) = 2 M(<r) exactly
    assert abs(enclosed_mass_for_flat_curve(220, 60) / enclosed_mass_for_flat_curve(220, 30) - 2.0) < 1e-12; n += 1
    # 5. keplerian: speed falls as r^-1/2 (doubling r divides v by sqrt 2)
    assert abs(keplerian_speed(1e41, 60) / keplerian_speed(1e41, 30) - 1/math.sqrt(2)) < 1e-12; n += 1
    # 6. DESI crossing for the quoted fit is z = 0.3/0.7
    assert abs(crossing_redshift(-0.7, -1.0) - 0.3/0.7) < 1e-12; n += 1
    # 7. failure paths: a cosmological constant (w0=-1, wa=0) has no crossing,
    #    and a fit that never reaches -1 (w0=-0.9, wa=+0.5) has none either
    assert crossing_redshift(-1.0, 0.0) is None; n += 1
    assert crossing_redshift(-0.9, 0.5) is None; n += 1
    print(f"selftest: {n}/8 passed (DM/baryon ratio ~5.4; Omega_m within Planck's error; rounded budget; flat-curve mass"
          f" doubles with r; Keplerian r^-1/2; DESI crossing z = 3/7; two no-crossing failure paths)")

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--selftest", action="store_true")
    a = ap.parse_args()
    selftest() if a.selftest else run()
