Add few functions and scaffold for Δv/T calculus

This commit is contained in:
Théophile Bastian 2024-09-01 14:23:23 +02:00
parent c72061107b
commit 3cf5382362

114
ksp_toolbox/const.py Normal file
View file

@ -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,
)