planetsfactory 0.0.1

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::*;
use crate::engine::orbits::OrbitalElements;

pub struct TerrestrialPlanet {
    pub name: String,
    pub parent: String,
    pub mass: f64,
    pub radius: f64,
    pub j2: f64,
    pub obliquity: f64,
    pub elements: OrbitalElements,
    pub albedo: f64,
    pub iron_core_fraction: f64,
}

impl TerrestrialPlanet {
    pub fn new(
        name: &str,
        parent: &str,
        mass: f64,
        radius: f64,
        j2: f64,
        obliquity: f64,
        elements: OrbitalElements,
    ) -> Self {
        Self {
            name: name.to_string(),
            parent: parent.to_string(),
            mass,
            radius,
            j2,
            obliquity,
            elements,
            albedo: 0.3,
            iron_core_fraction: 0.32,
        }
    }

    pub fn mercury() -> Self {
        Self {
            name: String::from("Mercury"),
            parent: String::from("Sun"),
            mass: 3.301_1e23,
            radius: 2.439_7e6,
            j2: 6.0e-5,
            obliquity: 0.034 * DEG,
            elements: OrbitalElements::from_au_deg(
                0.387_098, 0.205_630, 7.005, 48.331, 29.124, 174.796,
            ),
            albedo: 0.088,
            iron_core_fraction: 0.70,
        }
    }

    pub fn venus() -> Self {
        Self {
            name: String::from("Venus"),
            parent: String::from("Sun"),
            mass: 4.867_5e24,
            radius: 6.051_8e6,
            j2: 4.458e-6,
            obliquity: 177.36 * DEG,
            elements: OrbitalElements::from_au_deg(
                0.723_332, 0.006_772, 3.395, 76.680, 54.884, 50.416,
            ),
            albedo: 0.77,
            iron_core_fraction: 0.32,
        }
    }

    pub fn earth() -> Self {
        Self {
            name: String::from("Earth"),
            parent: String::from("Sun"),
            mass: 5.972_17e24,
            radius: 6.371e6,
            j2: 1.082_63e-3,
            obliquity: 23.439_3 * DEG,
            elements: OrbitalElements::from_au_deg(
                1.000_000, 0.016_710, 0.000_05, 348.739, 114.208, 357.517,
            ),
            albedo: 0.306,
            iron_core_fraction: 0.32,
        }
    }

    pub fn mars() -> Self {
        Self {
            name: String::from("Mars"),
            parent: String::from("Sun"),
            mass: 6.417_1e23,
            radius: 3.389_5e6,
            j2: 1.960_45e-3,
            obliquity: 25.19 * DEG,
            elements: OrbitalElements::from_au_deg(
                1.523_710, 0.093_394, 1.850, 49.558, 286.502, 19.412,
            ),
            albedo: 0.25,
            iron_core_fraction: 0.25,
        }
    }

    pub fn mass_earth(&self) -> f64 {
        self.mass / EARTH_MASS
    }

    pub fn radius_earth(&self) -> f64 {
        self.radius / EARTH_RADIUS
    }

    pub fn surface_gravity(&self) -> f64 {
        surface_gravity(self.mass, self.radius)
    }

    pub fn escape_velocity(&self) -> f64 {
        escape_velocity(self.mass, self.radius)
    }

    pub fn mean_density(&self) -> f64 {
        mean_density(self.mass, self.radius)
    }

    pub fn core_radius(&self) -> f64 {
        self.radius * self.iron_core_fraction.cbrt()
    }

    pub fn magnetic_moment_estimate(&self) -> f64 {
        let omega = 2.0 * std::f64::consts::PI / (86400.0);
        let r_core = self.core_radius();
        0.1 * (4.0 / 3.0 * std::f64::consts::PI * r_core.powi(3)) * 7874.0 * omega * r_core * r_core
    }

    pub fn tidal_locking_time(&self, star_mass: f64) -> f64 {
        let a = self.elements.a;
        let q = 100.0;
        let rigidity = 1e11;
        let omega0 = 2.0 * std::f64::consts::PI / 86400.0;
        omega0 * a.powi(6) * self.mass * rigidity * q
            / (3.0 * G * star_mass * star_mass * self.radius.powi(3))
    }

    pub fn to_cartesian(&self, parent_mass: f64) -> ([f64; 3], [f64; 3]) {
        crate::engine::orbits::elements_to_state(&self.elements, parent_mass + self.mass)
    }

    pub fn period(&self, parent_mass: f64) -> f64 {
        self.elements.period(G * (parent_mass + self.mass))
    }
}