Sunday, April 6, 2025

Start of a framework that will allow all the formulas and constants to be easily rescaled to any unit system.

 

Wanted to get my work I did today posted just in case.

This rescales a bunch of human choosen units into natural units I found inside the constants. 



import math


# --- Define Constants with SI values and units as lists of (unit, power) ---

constants = {

    "speed_of_light_c": {

        "value": 299792458,

        "units": [("m", 1), ("s", -1)], # m/s

        "symbol": "c"

    },

    "1 Hz": {

        "value": 1,

        "units": [("Hz", 1) ], 

        "symbol": "Hz"

    },

    "planck_constant_h": {

        "value": 6.62607015e-34,

        "units": [("kg", 1), ("m", 2), ("s", -1)], # J*s = kg*m^2/s^2 * s = kg*m^2/s

        "symbol": "h"

    },

    "boltzmann_constant_k": {

        "value": 1.380649e-23,

        "units": [("kg", 1), ("m", 2), ("s", -2), ("K", -1)], # J/K = kg*m^2/s^2 / K = kg*m^2/s^2/K

        "symbol": "k"

    },

    "gravitational_constant_G": {

        "value": 6.67430e-11,

        "units": [ ("m", 3), ("kg", -1), ("s", -2)], # N*m^2/kg^2 = (kg*m/s^2)*m^2/kg^2 = kg^-1*m^3*s^-2

        "symbol": "G"

    },

    "coulombs_constant_k_e": {

        "value": 8.987551787e9,

        "units": [("kg", 1), ("m", 3), ("s", -2), ("C", -2)], # N*m^2/C^2 = kg*m^3/(s^2*C^2)

        "symbol": "k_e"

    },

#Wien wavelength displacement law constant                   2.897 771 955... e-3

    "Wien wl d law": {

        "value": 2.897771955e-3,

        "units": [("m", 1), ("K", 1) ], 

        "symbol": "?"

    },

#neutron Compton wavelength                                  1.319 590 905 81 e-15    0.000 000 000 75 e-15    m

    "neutron Compton wavelength": {

        "value": 1.31959090581e-15,

        "units": [("m", 1) ],

        "symbol": "?"

    },

    "elementary_charge_e": {

        "value": 1.602176634e-19,

        "units": [("C", 1)], # Coulomb is treated as a base unit here for simplicity

        "symbol": "e"

    },

    "avogadro_constant_Na": {

        "value": 6.02214076e+23,

        "units": [("mol", -1)], # 1/mol is mol^-1

        "symbol": "Na"

    },

    "electron_mass_me": {

        "value": 9.1093837015e-31,

        "units": [("kg", 1)],

        "symbol": "me"

    },

    "proton_mass_mp": {

        "value": 1.67262192369e-27,

        "units": [("kg", 1)],

        "symbol": "mp"

    },

    "neutron_mass_mn": {

        "value": 1.67492749804e-27,

        "units": [("kg", 1)],

        "symbol": "mn"

    },

    "Angstrom star": {

        "value": 1.00001495e-10,

        "units": [("m", 1)],

        "symbol": "?"

    },

    "vacuum_permittivity_epsilon0": {

        "value": 8.8541878128e-12,

        "units": [("C", 2), ("kg", -1), ("m", -3), ("s", 2)], # F/m = C^2/J / m = C^2 / (kg*m^2/s^2) / m = C^2*s^2 / (kg*m^3)

        "symbol": "ε₀"

    },

    "vacuum_permeability_mu0": {

        "value": 1.25663706212e-6,

        "units": [("kg", 1), ("m", 1), ("s", -2), ("C", -2)], # H/m = V*s/A / m = (J/C)*s/(C/s) / m = J*s^2/C^2 / m = (kg*m^2/s^2)*s^2/C^2 / m = kg*m/C^2

        "symbol": "μ₀"

    },

    "rydberg_constant_Rydberg": {

        "value": 10973731.568160,

        "units": [("m", -1)], # 1/m is m^-1

        "symbol": "Rydberg"

    },

    "bohr_radius_a0": {

        "value": 5.29177210903e-11,

        "units": [("m", 1)],

        "symbol": "a₀"

    },

    "hartree_energy_Eh": {

        "value": 4.359744722071e-18,

        "units": [("kg", 1), ("m", 2), ("s", -2)], # J = kg*m^2/s^2

        "symbol": "E_h" # E_h

    },

    "electron_radius_re": {

        "value": 2.8179403262e-15,

        "units": [("m", 1)],

        "symbol": "r_e" # r_e

    },

    "klitzing_constant_Rk": {

        "value": 25812.80745,

        "units": [("kg", 1), ("m", 2), ("s", -3), ("C", -2)], # Ohm = kg*m^2/(s^3*A^2) = kg*m^2/(s^3*C^2/s^2) = kg*m^2/(s*C^2)

        "symbol": "R_K" # R_K

    },

     "weber_per_kelvin": { # Example of a derived unit combination, not a fundamental constant, but illustrative

        "value": 1.0, # Arbitrary value for demonstration

        "units": [("kg", 1), ("m", 2), ("s", -1), ("C", -1), ("K", -1)], # Weber per Kelvin, Wb/K = kg*m^2/(s^2*A) / K = kg*m^2/(s^2*C/s) / K = kg*m^2/(s*C*K)

        #"units": [("Wb", 1), ("K", -1)], # Weber per Kelvin, Wb/K = kg*m^2/(s^2*A) / K = kg*m^2/(s^2*C/s) / K = kg*m^2/(s*C*K)

        "symbol": "Wb/K"

    },

    "faraday_constant_F": {

        "value": 96485.33212,

        "units": [("mol", -1), ("C", 1)], # C/mol

        "symbol": "F"

    },

    "stefan_boltzmann_constant_sigma": {

        "value": 5.670374419e-8,

        "units": [("kg", 1), ("s", -3), ("K", -4)], # W/(m^2*K^4) = (J/s)/(m^2*K^4) = (kg*m^2/s^2)/s / (m^2*K^4) = kg/(s^3*K^4)

        "symbol": "σ"

    },

    "magnetic_flux_quantum_Phi0": {

        "value": 2.067833848e-15,

         # Wb = V*s = (J/C)*s = (kg*m^2/s^2)/C * s = kg*m^2/(s*C)

        "units": [("kg", 1), ("m", 2), ("s", -1), ("C", -1)],

        "symbol": "Φ₀"

    },

    "josephson_constant_KJ": {

        "value": 483597.8484e9,

        # Hz/V = 1/s / (J/C) = C / (s*J) = C / (s * kg*m^2/s^2) = C*s / (kg*m^2)

        "units": [("kg", -1), ("m", -2), ("s", 1), ("C", 1)],

        "symbol": "K_J"

    },

    "conductance_quantum_G0": {

        "value": 7.748091729e-5,

        # Siemens (S) = 1/Ohm = A/V = (C/s) / (J/C) = C^2 / (s*J) = C^2 / (s * kg*m^2/s^2) = C^2*s / (kg*m^2)

        "units": [("kg", -1), ("m", -2), ("s", 1), ("C", 2)],

        "symbol": "G₀"

    },

    "molar_gas_constant_R": {

        "value": 8.314462618,

        # J/(mol*K) = (kg*m^2/s^2) / (mol*K)

        "units": [("kg", 1), ("m", 2), ("s", -2), ("mol", -1), ("K", -1)],

        "symbol": "R"

    },

    "atomic_mass_unit_u": {

        "value": 1.66053906660e-27,

        "units": [("kg", 1)],

        "symbol": "u"

    },

    "bohr_magneton_muB": {

        "value": 9.2740100783e-24,

        # J/T = (kg*m^2/s^2) / (kg/(s*C)) = m^2*C/s

        "units": [("m", 2), ("s", -1), ("C", 1)], # Units are actually A⋅m² = (C/s)⋅m²

        "symbol": "μ_B"

    },

    "nuclear_magneton_muN": {

        "value": 5.0507837461e-27,

        # J/T, same units as Bohr Magneton

        "units": [("m", 2), ("s", -1), ("C", 1)], # Units are actually A⋅m² = (C/s)⋅m²

        "symbol": "μ_N"

    },

    "electron_compton_wavelength_lambda_eC": {

        "value": 2.42631023867e-12,

        "units": [("m", 1)],

        "symbol": "λ_e,C"

    },

    "planck_mass_mP": {

        "value": 2.176434e-8,

        "units": [("kg", 1)], # Derived from sqrt(hbar*c/G)

        "symbol": "m_P"

    },

    "planck_length_lP": {

        "value": 1.616255e-35,

        "units": [("m", 1)], # Derived from sqrt(hbar*G/c^3)

        "symbol": "l_P"

    },

    "planck_time_tP": {

        "value": 5.391247e-44,

        "units": [("s", 1)], # Derived from sqrt(hbar*G/c^5)

        "symbol": "t_P"

    },

    "planck_temperature_TP": {

        "value": 1.416784e+32,

        "units": [("K", 1)], # Derived from sqrt(hbar*c^5/(G*k^2))

        "symbol": "T_P"

    },

    "standard_gravity_g0": {

        "value": 9.80665,

        "units": [("m", 1), ("s", -2)], # m/s^2

        "symbol": "g₀"

    },

}


# --- Calculate Scaling Factors ---

c_value = constants["speed_of_light_c"]["value"]

h_value = constants["planck_constant_h"]["value"]

k_value = constants["boltzmann_constant_k"]["value"]

#k_value = 1.380649e-23 


# --- Rescaling Factors ---


# natural units

rescale_factors = {

    "m":  c_value,

    "kg": h_value / (c_value**2),

    "K":  h_value / k_value,

    "s":  1.0,

    "Hz": 1.0,

    "mol": 1/6.02214076e+23,

    "C":  1.602176634e-19,

}


'''

rescale_factors = {

    "m":  1.0,

    "kg": 1.0,

    "K":  1.0,

    "s":  1.0,

    "Hz": 1.0,

    "C":  1.0,

}

'''


'''

rescale_factors = {

    "m":  1.0,

    "kg": h_value / (c_value),

    "K":  h_value / k_value *c_value,

    "s":  1/c_value,

    "Hz":  c_value,

    "C":  1.602176634e-19,

}

'''


'''

# scaling units to powers of 10 for constants

rescale_factors = {

    "m":  c_value/1e10,

    "s":  1.0,

    "Hz": 1.0,

    "kg": h_value / (c_value**2) *1e10 ,

    #"kg": h_value / (c_value**2) * 1e51 ,

    "K":  1.0 ,

    #"K":  h_value / k_value ,

    "C":  1.602176634e-19,

}

'''


# --- set time to 1000 second, changes Hz, keep speed limit the same but with meters now 1km  ---

'''

rescale_factors = {

    "m":  1000.0,

    "s":  1000.0,

}

'''


# --- Function to Rescale a Value based on Unit List ---

def rescale_value_by_units_a(constant_data, rescale_factors):

    original_value = constant_data["value"]

    unit_list = constant_data["units"]

    rescaled_value = original_value


    for unit_symbol, power in unit_list:

        if unit_symbol in rescale_factors:

            rescaled_value /= (rescale_factors[unit_symbol] ** power) # Divide for numerator, multiply for denominator due to power

            print (f"{unit_symbol} {power} ", end="")


    print()

    return rescaled_value


# --- Function to Rescale a Value based on Unit List ---

def rescale_value_by_units(constant_data, rescale_factors):

    original_value = constant_data["value"]

    unit_list = constant_data["units"]

    rescaled_value = original_value

    units_applied_list = [] # Store the units that were actually scaled


    for unit_symbol, power in unit_list:

        if unit_symbol in rescale_factors:

            rescale_factor = rescale_factors[unit_symbol]

            rescaled_value /= (rescale_factor ** power) # Divide original value by the scaling factor raised to the power

            units_applied_list.append(f"{unit_symbol}^{power}") # Record the unit and power applied


    return rescaled_value, units_applied_list # Return both value and list of units applied



# --- Display Original and Rescaled Constants Side-by-Side ---

print(f"{'Constant':<30} {'Original Value':<25} {'Rescaled Value':<25}")

print("-" * 80)


for name, data in constants.items():

    original_value = data["value"]

    rescaled_value, units_applied = rescale_value_by_units(data, rescale_factors)

    units_applied_str = " ".join(units_applied)

    print(f"{data['symbol']:<10} {name:<35} {original_value:<20} {rescaled_value:20.10} {units_applied_str}")


print("\n--- Scaling Factors Used ---")

for unit_symbol, factor_value in rescale_factors.items():

    print(f"{unit_symbol:<10} {factor_value:.20e}")