planetsfactory 0.0.2

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 with_surface(mut self, albedo: f64, iron_core_fraction: f64) -> Self {
        self.albedo = albedo;
        self.iron_core_fraction = iron_core_fraction;
        self
    }

    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))
    }
}