"""
Test 8: Is the constant-H result physics or tautology?

We need to separate:
  - Energy conservation (continuity equation)
  - The dynamical equation that determines H (Friedmann analog)
  - The "radiative blocking" hypothesis and what it physically adds

Standard cosmology:
  Continuity: rho_dot + 3*H*(rho + p) = 0            [energy conservation]
  Friedmann:  H^2 = (8*pi*G/3) * rho                 [dynamics: gravity sources H]
  
These two together determine a(t) once you specify EOS (relation p = w*rho).

In Jay's brane picture, we need to ask what equation PLAYS THE ROLE of Friedmann.
Without that, we're not doing cosmology; we're doing energy bookkeeping.

Let me be honest about what I did in Test 7:
  - I wrote H = L/(3*w*rho), derived from setting "blocked radiation rate"
    equal to "mechanical work rate".
  - But this is just energy conservation rewritten. It tells you how rho
    relates to H given a specified L. It does NOT tell you what H is.
  - To get H, I'd need to know L independently.

So the question becomes: in the brane picture, is there a physical reason
to expect L ~ rho?

Option 1: blackbody radiation (L = sigma*T^4, rho_rad = a_B*T^4 => L ~ rho).
  But blackbody radiation only radiates if there's somewhere to radiate TO.
  If EM is brane-confined, there's no "outside" for blackbody photons to go to.
  So "L" here can't be blackbody in the usual sense.

Option 2: some intrinsic brane dissipation rate.
  The brane has internal dynamics. Maybe there's a natural rate of internal
  excitation loss (e.g., through gravitational coupling to the bulk, which
  IS allowed in Randall-Sundrum).
  If this rate is ~ rho (because gravitational coupling scales with energy),
  then L ~ rho naturally.

Option 3: purely formal.
  L ~ rho could just be DEFINED as "whatever dissipation channel the brane has",
  without specifying microphysics. The coefficient becomes a free parameter.
  This gives a one-parameter family with H = const (=de Sitter) as prediction,
  with H^2 set by the dissipation timescale.

OPTION 3 is essentially what the cosmological constant is in LCDM:
  a free parameter that happens to fit the data.

So is the brane picture just "reframing" the cosmological constant without
adding predictive content? Let me check.
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

print("=" * 70)
print("TEST 8: Physics or tautology?")
print("=" * 70)

# To distinguish physics from tautology, we ask:
# Does the brane picture give a RELATION between H and other observables
# that LCDM doesn't? Or is it just a rewording?

# Standard LCDM: H^2 = H0^2 * [Omega_m (1+z)^3 + Omega_Lambda]
# Lambda is a free parameter fit to data.

# Brane-cooling picture: H = L/(3w*rho_int)
# where L is the internal dissipation rate, rho_int is the "cooling" component,
# w is its EOS.
# If L ~ rho_int, H = const = 1/(3w*tau), where tau is the dissipation timescale.
# tau is a free parameter fit to data.

# Same number of free parameters! So no predictive gain at this level.

# BUT: the brane picture DOES make one new prediction.
# The brane can't radiate, but it CAN gravitationally couple to the bulk.
# The bulk coupling gives a leakage rate that depends on brane tension T_b
# and bulk geometry. This is not a free parameter in the same way.

# Also, there's a NON-TRIVIAL prediction:
# In LCDM, the cosmological constant Lambda is a constant.
# In the brane-cooling picture, IF the dissipation mechanism has any
# redshift-dependence (L ~ rho^n for n != 1), we predict a specific
# deviation from w = -1.

# Let's check what observational data says about w(z).

# Planck 2018 + SN + BAO:
# w = -1.028 +/- 0.031 (CPL parameterization w0 = w0 + wa*(1-a))
# Very consistent with w = -1.

# DESI 2024 (recent):
# w0 = -0.64 +/- 0.11, wa = -1.27 +/- 0.40
# Some tension with w = -1 at ~2-3 sigma level if wa != 0.

# So there's room for a non-constant w in the brane model.
# The specific prediction would be: if L ~ rho^n, then H = H0 * (rho/rho0)^(n-1)
# and w_eff = -1 - (2/3) * (n-1) * (rho_dot/rho) / H = -1 + something depending on n.

# Let me derive this carefully.

print("""
Derivation of w_eff in brane-cooling picture with L ~ rho^n:

  H = L/(3*w*rho) = (rho/rho0)^(n-1) / (3*w*tau0*rho0)
  
Let x = rho/rho0. Then H = x^(n-1) / (3*w*tau0*rho0).

Continuity: rho_dot = -3*(1+w)*rho*H
  =>  rho0 * x_dot = -3*(1+w) * rho0 * x * x^(n-1) / (3*w*tau0*rho0)
  =>  x_dot = -(1+w)/(w*tau0*rho0) * x^n
  
This is a simple ODE. Solution:
  For n = 1: x_dot = -(1+w)/(w*tau0*rho0) * x => exponential decay
  For n != 1: x^(1-n) = x0^(1-n) - (1-n)*(1+w)/(w*tau0*rho0) * t

Now H:
  H = x^(n-1) / (3*w*tau0*rho0)
  H_dot = (n-1)*x^(n-2) * x_dot / (3*w*tau0*rho0)
        = (n-1)/(3*w*tau0*rho0) * x^(n-2) * [-(1+w)/(w*tau0*rho0) * x^n]
        = -(n-1)(1+w)/(3*w^2*tau0^2*rho0^2) * x^(2n-2)
        = -(n-1)(1+w)/w * H^2 * 3*w*tau0*rho0 / (3*w*tau0*rho0)
  
Wait let me redo this. H = x^(n-1) * K where K = 1/(3*w*tau0*rho0).
So H^2 = x^(2n-2) * K^2.
And H_dot = (n-1) * x^(n-2) * x_dot * K
         = (n-1) * x^(n-2) * [-(1+w)/(w*tau0*rho0)] * x^n * K
         = -(n-1)*(1+w)/(w*tau0*rho0) * x^(2n-2) * K
         = -(n-1)*(1+w)/(w*tau0*rho0) * H^2 / K
         = -(n-1)*(1+w)*3 * H^2
         = -3*(n-1)*(1+w) * H^2
         
So H_dot/H^2 = -3*(n-1)*(1+w)

Effective EOS: w_eff satisfies
  H_dot/H^2 = -(3/2)*(1 + w_eff)
  => -3*(n-1)*(1+w) = -(3/2)*(1 + w_eff)
  => 2*(n-1)*(1+w) = 1 + w_eff
  => w_eff = 2*(n-1)*(1+w) - 1
  
For n = 1: w_eff = -1 (de Sitter). Confirmed.
For n > 1 and w > -1: w_eff > -1 (quintessence-like, H decreasing).
  Wait, that's not right. n > 1 should give "faster than exponential"...
  Let me re-examine.

Actually if n > 1 and w > -1, then (n-1)*(1+w) > 0, so H_dot/H^2 < 0, so H decreases.
This is quintessence-like behaviour (w_eff > -1).

If n < 1: H_dot/H^2 > 0, H increases, w_eff < -1 (phantom-like).

So the SIGN of deviation from w = -1 is determined by whether n > 1 or n < 1.
""")

# Test numerically for different n
print("Numerical verification:")
print(f"  {'n':<8}{'w_eff (theory)':<20}{'w_eff (numerical)':<20}")

w_internal = 1/3  # EOS of the internal dissipating component
for n in [0.5, 0.75, 1.0, 1.25, 1.5]:
    w_eff_theory = 2*(n-1)*(1+w_internal) - 1
    
    # Numerical
    tau0 = 1.0
    rho0 = 1.0
    def dydt(t, y):
        a, rho = y
        if rho <= 0:
            return [0, 0]
        H = (rho/rho0)**(n-1) / (3*w_internal*tau0*rho0)
        return [H*a, -3*(1+w_internal)*rho*H]
    
    sol = solve_ivp(dydt, [0, 5], [1.0, 1.0], rtol=1e-10, atol=1e-12,
                    dense_output=True, max_step=0.001)
    t = sol.t
    a = sol.y[0]
    rho = sol.y[1]
    H = (rho/rho0)**(n-1) / (3*w_internal*tau0*rho0)
    H_dot = np.gradient(H, t)
    # Use early times (before numerical drift)
    idx = len(t)//4
    w_eff_num = -1 - (2/3)*H_dot[idx]/H[idx]**2
    
    print(f"  {n:<8.2f}{w_eff_theory:<20.4f}{w_eff_num:<20.4f}")

# ==============================================================
# HONEST ASSESSMENT
# ==============================================================
print("\n" + "=" * 70)
print("HONEST ASSESSMENT: what the cooling-expansion picture actually gives")
print("=" * 70)
print("""
POSITIVE FINDINGS:
  1. The blocked-radiation hypothesis combined with energy conservation
     gives a SPECIFIC expansion history: H = (rho/rho0)^(n-1) * const
     where n is the exponent in the effective dissipation rate L ~ rho^n.
     
  2. For the physically natural case n = 1 (blackbody-analog scaling),
     this gives H = constant exactly - de Sitter behaviour, w_eff = -1.
     This is observationally the dominant feature of dark energy.
     
  3. Deviations from n = 1 give specific, testable deviations from w = -1,
     with definite SIGN: n > 1 => quintessence, n < 1 => phantom.
     This is a (weakly) falsifiable prediction if you can motivate n
     from microphysics.

NEGATIVE / HONEST LIMITATIONS:
  1. The derivation assumes the "blocked-radiation rate" is known
     independently of H. But without a Friedmann-analog dynamical equation,
     H is not determined by the brane dynamics alone - it's determined
     by energy conservation given an assumed L(rho).
     
  2. This means the brane picture is NOT adding a new dynamical law;
     it's postulating a relation (L ~ rho^n) that plays the same role
     as postulating a cosmological constant.
     
  3. The one-parameter family H(t) it gives is not OBSERVATIONALLY different
     from CPL-parameterized dark energy (which is also a one-parameter
     family around w = -1 with varying w(z)).
     
  4. What the brane picture offers that's DIFFERENT is an INTERPRETATION:
     dark energy is not a new substance but a consequence of the brane's
     inability to radiate EM energy into the bulk.
     This interpretation is not itself a dynamical prediction, but it does
     connect dark energy to the brane confinement postulate (which is
     independently testable via other channels, in principle).

RELATION TO SECTION 8.1 (the Lorentz problem):
  This cosmology result DOES NOT directly address the SO(4) vs SO(3,1)
  obstruction. It lives in a different sector of the theory (the large-scale
  dynamics of the brane itself) rather than in the kinematics of
  4-velocities.
  
  HOWEVER: a constant H defines a natural "de Sitter temperature"
  T_dS = H/(2*pi*k_B) (in natural units).
  This IS the kind of temperature that can play the role of the Matsubara
  imaginary-time periodicity.
  
  So there IS a potential connection to the deeper Lorentz-emergence route:
  the blocked-radiation cooling gives a de Sitter expansion, which gives
  a natural effective temperature, which gives the ingredient needed for
  Matsubara-style emergent Wick rotation.
  
  This is the non-trivial link: cosmology and Lorentz emergence are both
  symptoms of the same underlying fact (the brane has an effective
  temperature due to bulk geometry).
  
This is the next thread to pull.
""")
