orbital_mechanics.html
001| """Various Orbital Mechanics Functions.
001|
001| Functions:
001| surface SurfaceGravity ~get surface gravity given mass and radius
001| escape EscapeVelocity ~get escape velocity given mass and radius
001| period OrbitalPeriod ~get orbital period given mass and radius
001| distance OrbitalDistance ~get orbital radius given mass and period
001| velocityR OrbitalVelocityR ~get orbital velocity given mass and radius
001| velocityT OrbitalVelocityT ~get orbital velocity given radius and period
001|
001| Data:
001| G ~Newton's constant
001|
001| Information:
001| Earth: mass=5.972e24 kg, radius=6371 km
001| Moon: mass=7.346e22 kg, radius=1737 km
001|
001| """
002| from sys import argv
003| from math import sqrt, pi
004|
005| G = 6.67430e-11
006|
007| def SurfaceGravity (mass:float, radius:float) -> float:
008| '''Return surface gravity given body mass and radius.'''
009|
010| p = G * mass
011| q = pow(radius, 2)
012| return p/q
013|
014| def EscapeVelocity (mass:float, radius:float) -> float:
015| '''Return escape velocity given body mass and radius'''
016|
017| p = 2 * G * mass
018| q = radius
019| return sqrt(p/q)
020|
021| def OrbitalVelocityR (mass:float, radius:float) -> float:
022| '''Return orbital velocity given body mass and radius.'''
023|
024| p = G * mass
025| q = radius
026| return sqrt(p/q)
027|
028| def OrbitalPeriod (mass:float, radius:float) -> float:
029| '''Return orbital period given body mass and radius.'''
030|
031| p = pow(radius, 3)
032| q = G * mass
033| return 2 * pi * sqrt(p/q)
034|
035| def OrbitalVelocityT (radius:float, period:float) -> float:
036| '''Return orbital velocity given orbit radius and period.'''
037|
038| p = 2 * pi * radius
039| q = period
040| return p/q
041|
042| def OrbitalDistance (mass:float, period:float) -> float:
043| '''Return orbital radius given body mass and orbital period.'''
044|
045| p = G * mass * pow(period, 2)
046| q = 4 * pow(pi, 2)
047| return pow(p/q, 1/3)
048|
049|
050|
051| def main (*args):
052| """Parse commandline argument(s) and dispatch to appropriate function."""
053| command = args[0].lower() if 0 < len(args) else ''
054| arg1 = float(args[1]) if 1 < len(args) else 1.0
055| arg2 = float(args[2]) if 2 < len(args) else 100_000.0
056|
057| if command == 'surface': return SurfaceGravity(arg1, arg2)
058| if command == 'escape': return EscapeVelocity(arg1, arg2)
059| if command == 'period': return OrbitalPeriod(arg1, arg2)
060| if command == 'distance': return OrbitalDistance(arg1, arg2)
061| if command == 'velocityR': return OrbitalVelocityR(arg1, arg2)
062| if command == 'velocityT': return OrbitalVelocityT(arg1, arg2)
063|
064|
065| return 'Nothing to do.'
066|
067|
068| if __name__ == '__main__':
069| print(f'running: {argv[0]}')
070| print()
071|
072| rv = main(*argv[1:])
073| print(f'answer: {rv:,.3f}')
074| print()
075|
076| """eof"""
077|