From 3cf5382362588e6be0720418373c8eb15faff884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Sun, 1 Sep 2024 14:23:23 +0200 Subject: [PATCH] =?UTF-8?q?Add=20few=20functions=20and=20scaffold=20for=20?= =?UTF-8?q?=CE=94v/T=20calculus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ksp_toolbox/const.py | 114 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 ksp_toolbox/const.py diff --git a/ksp_toolbox/const.py b/ksp_toolbox/const.py new file mode 100644 index 0000000..d88f9b3 --- /dev/null +++ b/ksp_toolbox/const.py @@ -0,0 +1,114 @@ +import typing as t +from math import pi, sqrt + + +G = 6.67430e-11 + + +class Height(t.NamedTuple): + apoapsis: float + periapsis: float + + @property + def a(self) -> float: + return 0.5 * (self.apoapsis + self.periapsis) + + @classmethod + def circ(cls, h: float) -> "Height": + return cls(apoapsis=h, periapsis=h) + + +class Body(t.NamedTuple): + name: str + radius: float + parent: t.Optional["Body"] + mass: float + apoapsis: float + periapsis: float + inclination: float + + @property + def μ(self) -> float: + """Standard grav. parameter""" + return self.mass * G + + @property + def a(self) -> float: + """Semi-major axis""" + return 0.5 * (self.apoapsis + self.periapsis) + + def period_at(self, height: Height) -> float: + return 2 * pi * sqrt(height.a**3 / self.μ) + + def circ_height_for_period(self, period: float) -> float: + return (self.μ * period**2 / (4 * pi**2)) ** (1 / 3) + + def height_for_period(self, period: float, fixed_height: float) -> float: + return 2 * self.circ_height_for_period(period) - fixed_height + + def v_circ(self, r: float) -> float: + return sqrt(self.μ / r) + + def dv_transfer_ellipse(self, r1: float, r2: float) -> float: + """Δv to transfer from circular of radius r1 to elliptical with 2a=r1+r2""" + return self.v_circ(r1) * abs(sqrt(2 * r2 / (r1 + r2)) - 1) + + def dv_transfer(self, r1: float, r2: float) -> float: + """Δv to transfer from circular of radius r1 to circular of radius r2""" + return self.dv_transfer_ellipse(r1, r2) + self.dv_transfer_ellipse(r2, r1) + + +kerbol = Body( + name="kerbol", + radius=261.6e6, + parent=None, + mass=1.7565459e28, + apoapsis=0, + periapsis=0, + inclination=0, +) +kerbin = Body( + name="kerbin", + radius=600e3, + parent=kerbol, + mass=5.2915158e22, + apoapsis=13599840256, + periapsis=13599840256, + inclination=0, +) +mun = Body( + name="mun", + radius=200e3, + parent=kerbin, + mass=9.7599066e20, + apoapsis=12e6, + periapsis=12e6, + inclination=0, +) +minmus = Body( + name="minmus", + radius=60e3, + parent=kerbin, + mass=2.6457580e19, + apoapsis=47e6, + periapsis=47e6, + inclination=6, +) +eve = Body( + name="eve", + radius=700e3, + parent=kerbol, + mass=1.2243980e23, + apoapsis=9931011387, + periapsis=9734357701, + inclination=2.1, +) +gilly = Body( + name="gilly", + radius=13e3, + parent=eve, + mass=1.2420363e17, + apoapsis=48825000, + periapsis=14175000, + inclination=12, +)