"""
CLUMPING/VOID CHANNEL (the last physical lead kept under investigation). The smooth cosmic regime is x~3-6
(transition, gate~0.24, A~0.043, 40% short). But light crosses the real cosmic web: voids (rho<rhobar -> low local
acceleration -> x<1 -> deep-MOND, where the baryon-share gate s->sqrt(Omega_b/Omega_m)=0.396) and clusters
(x>>1 -> Newtonian, s->Omega_b/Omega_m=0.157). Does LOS-averaging through realistic clumping FORCE the gate up to
0.40? DECISIVE physics: is the screen MASS-weighted (trace source dS/dpsi ~ T^mu_mu ~ rho -> voids contribute
little -> stays low) or VOLUME-weighted (light path, equal comoving length per volume -> voids dominate -> high)?
Honesty-guard: report what the correct (trace=rho, mass-weighted) physics gives; don't assume volume-weighting saves it.
"""
import numpy as np
import dfd_params as P
a=P.ALPHA; twosqrta=2*np.sqrt(a)
f=P.OMEGA_B_H2/(P.OMEGA_M*(P.H0/100)**2)   # Omega_b/Omega_m
sqrtf=np.sqrt(f)
A_data=0.0686; gate_data=A_data/twosqrta

# AQUAL baryon-share s(X_tot,f): g(X)=a0*(X+sqrt(X^2+4X))/2 (mu=x/(1+x)); s=g(f*X)/g(X)
def gmap(X): return (X+np.sqrt(X*X+4*X))/2.0
def share(Xtot):
    Xtot=np.maximum(Xtot,1e-12)
    return gmap(f*Xtot)/gmap(Xtot)

X0 = np.sqrt(P.OMEGA_M)/twosqrta   # structure peculiar-accel x at MEAN density, z=0 = 3.06
print("="*82); print("CLUMPING/VOID CHANNEL — does LOS-averaging force the gate to sqrt(Ob/Om)=%.3f?"%sqrtf); print("="*82)
print(f"\n  f=Ob/Om={f:.4f}  sqrt(f)={sqrtf:.4f}  (deep-MOND ceiling)   Ob/Om={f:.4f} (Newtonian floor)")
print(f"  data gate={gate_data:.4f}  (A_data={A_data})   mean-density x0={X0:.3f}")
print(f"  share at mean density s(x0)={share(X0):.4f}  -> A={twosqrta*share(X0):.4f} (smooth, the 40% short)")

# lognormal volume-weighted density PDF: 1+delta = exp(sig*G - sig^2/2), G~N(0,1), <1+delta>_vol=1.
# local peculiar-accel x_local = x0 * (1+delta) (g_N,local ~ rho_local).  Sample.
print("\n  LOS-averaged share through a realistic clumpy field (x_local = x0*(1+delta)):")
print(f"  {'sigma(delta)':>12s} {'<s>_VOLUME':>11s} {'A_vol':>8s} {'<s>_MASS':>10s} {'A_mass':>8s}")
G=np.random.default_rng  # forbidden; use deterministic grid instead
# deterministic gaussian quadrature over G
gg=np.linspace(-6,6,4001); wG=np.exp(-gg*gg/2)/np.sqrt(2*np.pi); wG/=wG.sum()
for sig in (0.5,1.0,1.5,2.0,2.5):
    opd = np.exp(sig*gg - sig*sig/2)          # 1+delta, volume-weighted lognormal (<opd>_vol=1)
    Xloc = X0*opd
    s = share(Xloc)
    s_vol = np.sum(s*wG)                        # volume-weighted (light path)
    s_mass= np.sum(s*opd*wG)/np.sum(opd*wG)     # mass-weighted (trace source ~ rho)
    print(f"  {sig:12.2f} {s_vol:11.4f} {twosqrta*s_vol:8.4f} {s_mass:10.4f} {twosqrta*s_mass:8.4f}")

print("\n  " + "-"*78)
print("  PHYSICS VERDICT: the DFD screen source is the TRACE dS/dpsi ~ T^mu_mu ~ rho_b -> MASS-weighted.")
print("  Mass-weighting puts the weight on dense (high-x, Newtonian, s->f=%.3f) regions -> gate STAYS LOW"%f)
print("  (A_mass well below 0.0686). Only VOLUME-weighting (which would require the screen to be sourced by")
print("  path-length not mass) pushes toward sqrt(f); but that contradicts the trace coupling. So the clumping")
print("  channel does NOT force 0.40 under the correct (mass/trace) weighting. Voids are deep-MOND but the")
print("  trace source barely lives there. Honest: clumping is tunable (via weighting choice), NOT forced.")
