#!/usr/bin/env python3
"""
3D+3D THEORY CONSTANTS
======================

Central repository for all physical constants and 3D+3D parameters.
Import this module in all analysis scripts for consistency.

All parameters are DERIVED from fundamental constants and geometry.
NO FREE PARAMETERS - this is the key distinguishing feature of 3D+3D theory.

Author: Simone Calzighetti & Lucy (Claude AI)
Date: December 2025
Version: 1.0
"""

import numpy as np

# ============================================================================
# MATHEMATICAL CONSTANTS
# ============================================================================

PI = np.pi
PHI = (1 + np.sqrt(5)) / 2  # Golden ratio = 1.6180339887...
PHI_SQUARED = PHI ** 2      # = 2.6180339887...
PHI_INV = 1 / PHI           # = 0.6180339887...

# Fibonacci sequence (for reference)
FIBONACCI = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

# ============================================================================
# FUNDAMENTAL PHYSICAL CONSTANTS (CGS)
# ============================================================================

class CGS:
    """CGS unit system constants."""
    
    G = 6.67430e-8          # Gravitational constant [cm³/(g·s²)]
    c = 2.99792458e10       # Speed of light [cm/s]
    hbar = 1.054571817e-27  # Reduced Planck constant [erg·s]
    k_B = 1.380649e-16      # Boltzmann constant [erg/K]
    
    # Mass scales
    M_sun = 1.98892e33      # Solar mass [g]
    M_earth = 5.9722e27     # Earth mass [g]
    M_Pl = 2.17643e-5       # Planck mass [g]
    
    # Length scales
    pc = 3.0857e18          # Parsec [cm]
    kpc = 3.0857e21         # Kiloparsec [cm]
    Mpc = 3.0857e24         # Megaparsec [cm]
    AU = 1.49598e13         # Astronomical unit [cm]
    ly = 9.4607e17          # Light-year [cm]
    
    # Time scales
    year = 3.15576e7        # Year [s]
    
    # Energy conversion
    eV = 1.602176634e-12    # Electron-volt [erg]
    GeV = 1.602176634e-3    # Giga electron-volt [erg]

# ============================================================================
# SI UNITS
# ============================================================================

class SI:
    """SI unit system constants."""
    
    G = 6.67430e-11         # [m³/(kg·s²)]
    c = 2.99792458e8        # [m/s]
    hbar = 1.054571817e-34  # [J·s]
    
    M_sun = 1.98892e30      # [kg]
    kpc = 3.0857e19         # [m]
    Mpc = 3.0857e22         # [m]

# ============================================================================
# NATURAL UNITS (ℏ = c = 1)
# ============================================================================

class Natural:
    """Natural units with ℏ = c = 1."""
    
    # Planck units
    M_Pl = 1.22089e19       # Planck mass [GeV]
    M_Pl_reduced = 2.435e18 # Reduced Planck mass [GeV]
    L_Pl = 1.616255e-35     # Planck length [m]
    t_Pl = 5.391247e-44     # Planck time [s]
    
    # Conversion factors
    hbar_c = 0.1973269804   # ℏc [GeV·fm]
    hbar_c_kpc = 6.4e-27    # ℏc [eV·kpc]

# ============================================================================
# 3D+3D FUNDAMENTAL PARAMETERS
# ============================================================================

class Geometry3D3D:
    """
    Fundamental geometric parameters of 3D+3D spacetime.
    
    Spacetime signature: (-,+,+,+,-,-)
    Three spatial dimensions: (x, y, z)
    Three temporal dimensions: (t, τ₂, τ₃)
    """
    
    # Metric signature
    SIGNATURE = (-1, +1, +1, +1, -1, -1)
    
    # Compactification radii (temporal dimensions)
    L4 = 15.1   # [light-years] - τ₂ compactification radius
    L5 = 9.6    # [light-years] - τ₃ compactification radius
    
    # Period ratio
    L_RATIO = L4 / L5  # = 1.573 ≈ φ
    
    # Effective temporal periods
    T2 = 30.0   # [years] - τ₂ breathing period
    T3 = 19.0   # [years] - τ₃ breathing period
    T_RATIO = T2 / T3  # = 1.579 ≈ φ

# ============================================================================
# Q-FIELD PARAMETERS
# ============================================================================

class QField:
    """
    Q-field parameters derived from compactification.
    
    Q₂ couples to τ₂, Q₃ couples to τ₃.
    Masses derived from: m_i = ℏ/(c·L_i)
    """
    
    # Q-field masses [eV]
    m2 = 1.47e-24    # Q₂ mass (from L₄)
    m3 = 2.32e-24    # Q₃ mass (from L₅)
    
    # Alternative values (different derivation)
    m2_alt = 4.37e-24
    m3_alt = 6.90e-24
    
    # Mass ratio
    MASS_RATIO = m3 / m2  # ≈ 1.58 ≈ φ
    
    # Universal coupling constant
    BETA = 0.49          # Dimensionless coupling β
    BETA_2 = 3.0         # β₂ (alternative parameterization)
    BETA_3 = 2.0         # β₃
    
    # Vainshtein parameters
    LAMBDA_VAINSHTEIN = 1e-13  # [eV] - Vainshtein scale
    
    @classmethod
    def compton_wavelength(cls, mass_eV):
        """Compton wavelength in kpc."""
        hbar_c = Natural.hbar_c_kpc
        return hbar_c / mass_eV

# ============================================================================
# BREATHING SCALE LADDER
# ============================================================================

class BreathingScales:
    """
    Harmonic breathing scale ladder.
    
    λₙ = λ₂ × φ^(n-2)
    
    These scales emerge as eigenvalues of the coupled Q₂-Q₃ system.
    """
    
    # Fundamental scale (from SPARC rotation curves)
    LAMBDA_2 = 4.30     # [kpc] - n=2, fundamental
    
    # Critical mass at λ₂
    M_CRIT_2 = 2.43e10  # [M_sun]
    
    # Pre-computed key scales
    SCALES = {
        0: {'lambda_kpc': 4.30 / PHI**2,   'name': 'Dwarf cores'},
        1: {'lambda_kpc': 4.30 / PHI,      'name': 'Inner disks'},
        2: {'lambda_kpc': 4.30,            'name': 'FUNDAMENTAL (SPARC)'},
        3: {'lambda_kpc': 4.30 * PHI,      'name': 'Outer disks'},
        4: {'lambda_kpc': 4.30 * PHI**2,   'name': 'Massive galaxies (SLACS)'},
        5: {'lambda_kpc': 4.30 * PHI**3,   'name': 'Galaxy groups'},
        6: {'lambda_kpc': 4.30 * PHI**4,   'name': 'Poor clusters'},
        7: {'lambda_kpc': 4.30 * PHI**5,   'name': 'Rich clusters'},
        13: {'lambda_kpc': 4.30 * PHI**11, 'name': 'Cosmic web filaments'},
    }
    
    # Cosmic web scale
    LAMBDA_13 = 4.30 * PHI**11 / 1000  # [Mpc] = 0.856 Mpc
    
    @classmethod
    def get_lambda(cls, n):
        """Get breathing scale λₙ [kpc]."""
        return cls.LAMBDA_2 * PHI**(n - 2)
    
    @classmethod
    def get_M_crit(cls, n):
        """Get critical mass M_crit(λₙ) [M_sun]."""
        return cls.M_CRIT_2 * PHI**(2*(n - 2))
    
    @classmethod
    def print_ladder(cls, n_max=15):
        """Print the complete scale ladder."""
        print("\n" + "="*70)
        print("3D+3D BREATHING SCALE LADDER")
        print("="*70)
        print(f"\nλₙ = λ₂ × φ^(n-2), where λ₂ = {cls.LAMBDA_2} kpc")
        print("-"*70)
        print(f"{'n':>3} | {'λₙ [kpc]':>12} | {'λₙ [Mpc]':>10} | {'M_crit [M☉]':>14}")
        print("-"*70)
        
        for n in range(n_max + 1):
            lam = cls.get_lambda(n)
            M_crit = cls.get_M_crit(n)
            print(f"{n:>3} | {lam:>12.3f} | {lam/1000:>10.5f} | {M_crit:>14.2e}")
        
        print("-"*70)

# ============================================================================
# COSMOLOGICAL PARAMETERS
# ============================================================================

class Cosmology:
    """
    Cosmological parameters (Planck 2018).
    """
    
    H0 = 67.4           # Hubble constant [km/s/Mpc]
    h = 0.674           # Reduced Hubble parameter
    
    Omega_m = 0.315     # Matter density
    Omega_b = 0.0493    # Baryon density
    Omega_c = 0.265     # Cold dark matter density (ΛCDM)
    Omega_L = 0.685     # Dark energy density
    Omega_k = 0.0       # Curvature (flat)
    
    # Derived quantities
    rho_crit_0 = 9.47e-30  # Critical density [g/cm³]
    t_0 = 13.8e9           # Age of universe [years]
    
    # Sound horizon at drag epoch
    r_s = 147.09  # [Mpc] (for BAO)
    
    # Note: In 3D+3D theory, Omega_c (dark matter) is replaced
    # by Q-field effects. Effective Omega_Q can be computed.

# ============================================================================
# OBSERVATIONAL TARGETS
# ============================================================================

class Observations:
    """
    Key observational values for validation.
    """
    
    # SPARC rotation curves
    SPARC_RMS_TARGET = 25.0        # [km/s] typical RMS
    SPARC_N_GALAXIES = 175
    
    # SLACS gravitational lensing
    SLACS_R_RATIO = 0.749          # θ_E,3D3D / θ_E,GR
    SLACS_R_ERROR = 0.034
    SLACS_N_LENSES = 73
    
    # NANOGrav pulsar timing
    NANOGRAV_T2 = 30.0             # [years]
    NANOGRAV_T3 = 19.0             # [years]
    
    # DESI cosmic web
    DESI_LAMBDA_13 = 0.85          # [Mpc] filament spacing
    DESI_LAMBDA_13_ERROR = 0.20
    
    # Euclid predictions (pre-registered)
    EUCLID_LAMBDA_13_PRED = 0.856  # [Mpc]
    EUCLID_LAMBDA_13_TOL = 0.10    # ±10%

# ============================================================================
# UNIT CONVERSIONS
# ============================================================================

def eV_to_inv_kpc(mass_eV):
    """Convert mass in eV to inverse length in kpc⁻¹."""
    return mass_eV / Natural.hbar_c_kpc

def inv_kpc_to_eV(k_inv_kpc):
    """Convert wavenumber in kpc⁻¹ to energy in eV."""
    return k_inv_kpc * Natural.hbar_c_kpc

def km_s_to_kpc_Gyr(v_km_s):
    """Convert velocity from km/s to kpc/Gyr."""
    return v_km_s * 1.0227  # 1 km/s ≈ 1.0227 kpc/Gyr

def Msun_to_g(M_Msun):
    """Convert mass from solar masses to grams."""
    return M_Msun * CGS.M_sun

def kpc_to_cm(r_kpc):
    """Convert length from kpc to cm."""
    return r_kpc * CGS.kpc

# ============================================================================
# QUICK REFERENCE
# ============================================================================

def print_summary():
    """Print summary of key 3D+3D parameters."""
    
    print("\n" + "="*70)
    print("3D+3D THEORY - KEY PARAMETERS")
    print("="*70)
    
    print("\n[GEOMETRY]")
    print(f"  Signature: (-,+,+,+,-,-)")
    print(f"  L₄ = {Geometry3D3D.L4} ly (τ₂ radius)")
    print(f"  L₅ = {Geometry3D3D.L5} ly (τ₃ radius)")
    print(f"  L₄/L₅ = {Geometry3D3D.L_RATIO:.3f} ≈ φ")
    
    print("\n[Q-FIELDS]")
    print(f"  m₂ = {QField.m2:.2e} eV")
    print(f"  m₃ = {QField.m3:.2e} eV")
    print(f"  β  = {QField.BETA}")
    
    print("\n[BREATHING SCALES]")
    print(f"  λ₂ = {BreathingScales.LAMBDA_2} kpc (fundamental)")
    print(f"  λ₄ = {BreathingScales.get_lambda(4):.1f} kpc (SLACS)")
    print(f"  λ₁₃ = {BreathingScales.LAMBDA_13:.3f} Mpc (cosmic web)")
    
    print("\n[TEMPORAL PERIODS]")
    print(f"  T₂ = {Geometry3D3D.T2} years")
    print(f"  T₃ = {Geometry3D3D.T3} years")
    print(f"  T₂/T₃ = {Geometry3D3D.T_RATIO:.3f} ≈ φ = {PHI:.3f}")
    
    print("\n[GOLDEN RATIO]")
    print(f"  φ = {PHI:.10f}")
    print(f"  φ² = {PHI_SQUARED:.10f}")
    print(f"  1/φ = {PHI_INV:.10f}")
    
    print("\n" + "="*70)

# ============================================================================
# MAIN
# ============================================================================

if __name__ == '__main__':
    print_summary()
    print()
    BreathingScales.print_ladder()
