"""Newtonian gravity and Schwarzschild black holes.""" from __future__ import annotations import math import numpy as np from zenith.engines.base import Param, Result, SpecError, finite, series, validate_params from zenith.engines.constants import AU, C, G, M_EARTH, M_SUN, R_EARTH SCHEMAS: dict[str, list[Param]] = { "force": [ Param("m1_kg", "kg", 1.0, 1e45, M_SUN, description="mass of the first body"), Param("m2_kg", "kg", 1e-30, 1e45, M_EARTH, description="mass of the second body"), Param("r_m", "m", 1e-3, 1e27, AU, description="distance between their centres"), ], "escape": [ Param("mass_msun", "M☉", 1e-12, 1e12, None, description="body mass in solar masses"), Param("mass_mearth", "M⊕", 1e-6, 1e8, None, description="body mass in Earth masses"), Param("radius_km", "km", 1e-3, 1e12, R_EARTH / 1e3, description="body radius"), ], "black_hole": [ Param("mass_msun", "M☉", 1e-8, 1e11, 1.0, description="black hole mass"), Param("r_rs", "r_s", 1.0001, 1e9, 10.0, description="observer distance in Schwarzschild radii"), ], } EXCLUSIVE = {"escape": [("mass_msun", "mass_mearth")]} def schwarzschild_m(mass_kg: float) -> float: return 2 * G * mass_kg / C**2 def run(op: str, params: dict) -> Result: p = validate_params(SCHEMAS[op], params, EXCLUSIVE.get(op, ())) if op == "force": f = G * p["m1_kg"] * p["m2_kg"] / p["r_m"] ** 2 values = {"force_n": f, "accel_m1_ms2": f / p["m1_kg"], "accel_m2_ms2": f / p["m2_kg"]} return Result("gravity", op, p, finite(values), {}, []) if op == "escape": if "mass_msun" in p: mass = p["mass_msun"] * M_SUN else: mass = p.get("mass_mearth", 1.0) * M_EARTH p["mass_mearth"] = mass / M_EARTH r = p["radius_km"] * 1e3 if r <= schwarzschild_m(mass): raise SpecError("radius is inside the event horizon: this object would be a black hole") v = math.sqrt(2 * G * mass / r) values = {"escape_kms": v / 1e3, "surface_g_ms2": G * mass / r**2} notes = ["relativistic_regime"] if v > 0.1 * C else ["newtonian_approximation"] return Result("gravity", op, p, finite(values), {}, notes) rs = schwarzschild_m(p["mass_msun"] * M_SUN) x = np.logspace(math.log10(1.0001), 2, 200) values = { "schwarzschild_km": rs / 1e3, "time_dilation": math.sqrt(1 - 1 / p["r_rs"]), "photon_sphere_km": 1.5 * rs / 1e3, "isco_km": 3 * rs / 1e3, } plots = {"dilation_vs_r": series(x, np.sqrt(1 - 1 / x), "distance (Schwarzschild radii)", "clock rate vs far away")} return Result("gravity", op, p, finite(values), plots, ["relativistic_regime"])