planetsfactory 0.0.3

Planet factory — classify, build and catalogue planets for any star system: Solar System, TRAPPIST-1, Kepler-90, Proxima Centauri, or fully custom worlds.
Documentation
use crate::config::parameters::{G, K_B, SIGMA_SB};
use std::f64::consts::PI;

pub struct Atmosphere {
    pub surface_pressure: f64,
    pub surface_temperature: f64,
    pub mean_molecular_mass: f64,
    pub scale_height: f64,
}

impl Atmosphere {
    pub fn new(
        surface_pressure: f64,
        surface_temperature: f64,
        mean_molecular_mass: f64,
        planet_mass: f64,
        planet_radius: f64,
    ) -> Self {
        let g = G * planet_mass / (planet_radius * planet_radius);
        let h = K_B * surface_temperature / (mean_molecular_mass * g);
        Self {
            surface_pressure,
            surface_temperature,
            mean_molecular_mass,
            scale_height: h,
        }
    }

    pub fn pressure_at_altitude(&self, h: f64) -> f64 {
        self.surface_pressure * (-h / self.scale_height).exp()
    }

    pub fn density_at_altitude(&self, h: f64) -> f64 {
        let p = self.pressure_at_altitude(h);
        p * self.mean_molecular_mass / (K_B * self.surface_temperature)
    }

    pub fn column_mass(&self, planet_mass: f64, planet_radius: f64) -> f64 {
        let g = G * planet_mass / (planet_radius * planet_radius);
        self.surface_pressure / g
    }
}

pub fn jeans_escape_parameter(
    planet_mass: f64,
    planet_radius: f64,
    exo_temperature: f64,
    particle_mass: f64,
) -> f64 {
    G * planet_mass * particle_mass / (K_B * exo_temperature * planet_radius)
}

pub fn jeans_escape_rate(
    planet_mass: f64,
    planet_radius: f64,
    exo_temperature: f64,
    particle_mass: f64,
    exo_density: f64,
) -> f64 {
    let lambda = jeans_escape_parameter(planet_mass, planet_radius, exo_temperature, particle_mass);
    let v_th = (2.0 * K_B * exo_temperature / particle_mass).sqrt();
    let area = 4.0 * PI * planet_radius * planet_radius;
    0.25 * exo_density * v_th * area * (1.0 + lambda) * (-lambda).exp()
}

pub fn greenhouse_temperature_rise(optical_depth: f64) -> f64 {
    (1.0 + 0.75 * optical_depth).powf(0.25)
}

pub fn effective_temperature(luminosity: f64, distance: f64) -> f64 {
    let flux = luminosity / (4.0 * PI * distance * distance);
    (flux / SIGMA_SB).powf(0.25)
}

pub fn atmospheric_scale_height(
    temperature: f64,
    mean_molecular_mass: f64,
    surface_gravity: f64,
) -> f64 {
    K_B * temperature / (mean_molecular_mass * surface_gravity)
}

pub fn bond_albedo_to_greenhouse(bond_albedo: f64, surface_temp: f64, eq_temp: f64) -> f64 {
    let t_ratio = surface_temp / eq_temp;
    let optical_depth = (t_ratio.powi(4) - 1.0) / 0.75;
    let _ = bond_albedo;
    optical_depth.max(0.0)
}