orbits.html
001| '''Orbits.
001|
001| Functions:
001|
001| Commands:
001| 1 do_list_orbital_parameters -List of orbital parameters.
001| 2 do_orbit_radius_by_mass -List orbit radii given a mass.
001| 3 do_planet_surface_orbits -List of planetary surface orbit periods.
001| 4 do_orbit_with_period_and_mass -
001|
001| ================================================================================================
001| Earth Mass: 5.972e+24 kilograms
001| Earth Radius: 6.371e+06 meters
001| Earth Circum: 4.008e+07 meters
001| Earth Period: 8.640e+04 seconds (1440.000 mins)
001|
001| Earth Surface Gravity: 9.820582 m/s^2
001| Earth Escape Velocity: 11.19 km/s (25,023 MPH)
001| Earth Max Surface Speed: 4.638e-01 km/s (1,038 MPH)
001| Elevator Towertop Speed: 4.721e-01 km/s (1,056 MPH)
001|
001| Orbit | Altitude (km) | Radius (km) | Period (min) | Speed (km/s) | Speed (MPH)
001| ---------- | ------------- | -------------- | -------------- | --------------- | ---------------
001| Special 1 | 526 km | 526 km | 2.000 min | 27,532.798 km/s | 61,589.217 MPH
001| Special 2 | 689 km | 689 km | 3.000 min | 24,052.115 km/s | 53,803.137 MPH
001| Special 3 | 969 km | 969 km | 5.000 min | 20,286.339 km/s | 45,379.323 MPH
001| Special 4 | 1,538 km | 1,538 km | 10.000 min | 16,101.278 km/s | 36,017.593 MPH
001| ---------- | ------------- | -------------- | -------------- | --------------- | ---------------
001| Surface | 0 km | 6,371 km | 84.346 min | 7,909.926 km/s | 17,694.030 MPH
001| Catch (lo) | 3 km | 6,374 km | 84.406 min | 7,908.034 km/s | 17,689.798 MPH
001| Catch (hi) | 11 km | 6,382 km | 84.558 min | 7,903.312 km/s | 17,679.234 MPH
001| Elevator | 121 km | 6,492 km | 86.754 min | 7,836.046 km/s | 17,528.765 MPH
001| LEO (lo) | 200 km | 6,571 km | 88.349 min | 7,788.619 km/s | 17,422.674 MPH
001| LEO (hi) | 2,000 km | 8,371 km | 127.034 min | 6,900.611 km/s | 15,436.253 MPH
001| Geosync | 35,786 km | 42,157 km | 1,435.678 min | 3,074.973 km/s | 6,878.531 MPH
001| Moon | 362,600 km | 368,971 km | 36,215.468 min | 1,048.485 km/s | 2,345.399 MPH
001| ================================================================================================
001| mercury 85.02 mins @ 3.01 km/s [3.30e+23 kg @ 2439.70 km]
001| venus 86.50 mins @ 7.33 km/s [4.87e+24 kg @ 6051.80 km]
001| earth 84.35 mins @ 7.91 km/s [5.97e+24 kg @ 6371.00 km]
001| moon 108.33 mins @ 1.68 km/s [7.34e+22 kg @ 1737.40 km]
001| mars 99.85 mins @ 3.55 km/s [6.42e+23 kg @ 3389.50 km]
001| jupiter 171.98 mins @ 42.57 km/s [1.90e+27 kg @ 69911.00 km]
001| saturn 238.93 mins @ 25.52 km/s [5.68e+26 kg @ 58232.00 km]
001| uranus 175.72 mins @ 15.11 km/s [8.68e+25 kg @ 25362.00 km]
001| neptune 154.75 mins @ 16.66 km/s [1.02e+26 kg @ 24622.00 km]
001| pluto 145.46 mins @ 0.86 km/s [1.30e+22 kg @ 1188.30 km]
001| ================================================================================================
001| https://en.wikipedia.org/wiki/Orbital_speed
001| https://en.wikipedia.org/wiki/Orbital_period
001| '''
002|
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from math import sqrt, pi
007| from logger import logger,info,debug,trace, LogFilenameTimestamp
008| from goodies import KWARG
009| from files import linefile
010| from facts import Planets
011|
012| BasePath = r'C:\CJS\prj\Python\app\maths'
013| Log = logger('Orbits')
014|
015| CMND = ''
016| ARGS = {
017|
018|
019|
020|
021|
022| }
023|
024|
025| G = 6.67430e-11
026|
027| M_sun = Planets['sun']['mass']['kg']
028| R_sun = Planets['sun']['radius']['km']['mean'] * 1000
029|
030| M_moon = Planets['moon']['mass']['kg']
031| R_moon = Planets['moon']['radius']['km']['mean'] * 1000
032| D_moon = Planets['moon']['orbit']['distance']['km']['min'] * 1000
033|
034| M_earth = Planets['earth']['mass']['kg']
035| R_earth = Planets['earth']['radius']['km']['mean'] * 1000
036| C_earth = Planets['earth']['circum']['km'] * 1000
037| T_earth = 24 * 60 * 60
038| D_earth = Planets['earth']['orbit']['distance']['km']['min'] * 1000
039|
040| D_iss = ((413+422)/2) * 1000
041|
042| mps2MPH = lambda mps: (mps * 2.23694)
043|
044|
045|
046| def SurfaceGravity (mass, radius):
047| '''Return surface gravity given body mass and radius.'''
048|
049| p = G * mass
050| q = pow(radius, 2)
051| return p/q
052|
053| G_sun = SurfaceGravity(M_sun, R_sun)
054| G_earth = SurfaceGravity(M_earth, R_earth)
055|
056|
057| def EscapeVelocity (mass, radius):
058| '''Return escape velocity given body mass and radius'''
059|
060| p = 2 * G * mass
061| q = radius
062| return sqrt(p/q)
063|
064| EV_sun = EscapeVelocity(M_sun, R_sun)
065| EV_earth = EscapeVelocity(M_earth, R_earth)
066|
067|
068| def OrbitalVelocityR (mass, radius):
069| '''Return orbital velocity given body mass and radius.'''
070|
071| p = G * mass
072| q = radius
073| return sqrt(p/q)
074|
075|
076| def OrbitalPeriod (mass, radius):
077| '''Return orbital period given body mass and radius.'''
078|
079| p = pow(radius, 3)
080| q = G * mass
081| return 2 * pi * sqrt(p/q)
082|
083|
084| def OrbitalVelocityT (radius, period):
085| '''Return orbital velocity given orbit radius and period.'''
086|
087| p = 2 * pi * radius
088| q = period
089| return p/q
090|
091|
092| def OrbitalDistance (mass, period):
093| '''Return orbital radius given body mass and orbital period.'''
094|
095| p = G * mass * pow(period, 2)
096| q = 4 * pow(pi, 2)
097| return pow(p/q, 1/3)
098|
099|
100|
101| def do_list_orbital_parameters (**kwargs):
102| """CMND: Generate a list of orbital parameters."""
103| fpath = KWARG('fpath', BasePath, str, kwargs)
104| fname = KWARG('fname', 'params.list', str, kwargs)
105| Log.info('orbital-parameters:: %s' % str(list(kwargs.keys())))
106| Log.debug('fpath="%s"' % fpath)
107| Log.debug('fname="%s"' % fname)
108| Log.info()
109|
110| def table_line (n, a=None, t=None, v=None, r=R_earth):
111| if n:
112| if n.startswith('--'):
113| n = '-'*10
114| s1 = '-'*13
115| s2 = '-'*14
116| s3 = '-'*14
117| s4 = '-'*15
118| s5 = '-'*15
119| else:
120| s1 = '' if a is None else f'{a/1000:,.0f} km'
121| s2 = '' if a is None else f'{(a+r)/1000:,.0f} km'
122| s3 = '' if t is None else f'{t:,.3f} min'
123| s4 = '' if v is None else f'{v:,.3f} km/s'
124| s5 = '' if v is None else f'{mps2MPH(v):,.3f} MPH'
125| else:
126| n = 'Orbit'
127| s1 = 'Altitude (km)'
128| s2 = 'Radius (km)'
129| s3 = 'Period (min)'
130| s4 = 'Speed (km/s)'
131| s5 = 'Speed (MPH)'
132| Log.info(f'{n:<10s} | {s1:>13s} | {s2:>14s} | {s3:>14s} | {s4:>15s} | {s5:>15s}')
133|
134| Log.info(f'Earth Mass: {M_earth:.3e} kilograms')
135| Log.info(f'Earth Radius: {R_earth:.3e} meters')
136| Log.info(f'Earth Circum: {C_earth:.3e} meters')
137| Log.info(f'Earth Period: {T_earth:.3e} seconds ({T_earth/60:.3f} mins)')
138| Log.info()
139|
140|
141| g_earth = SurfaceGravity(M_earth, R_earth)
142| Log.info(f'Earth Surface Gravity: {g_earth:.6f} m/s^2')
143|
144| ev_earth = EscapeVelocity(M_earth, R_earth) / 1000
145| Log.info(f'Earth Escape Velocity: {ev_earth:.2f} km/s ({mps2MPH(1000*ev_earth):,.0f} MPH)')
146|
147| vs_earth = (C_earth / T_earth) / 1000
148| vs_elev = ((2*pi*(R_earth+120_700)) / T_earth) / 1000
149| Log.info(f'Earth Max Surface Speed: {vs_earth:.3e} km/s ({mps2MPH(1000*vs_earth):,.0f} MPH)')
150| Log.info(f'Elevator Towertop Speed: {vs_elev:.3e} km/s ({mps2MPH(1000*vs_elev):,.0f} MPH)')
151| Log.info()
152|
153|
154| v_earth = OrbitalVelocityR(M_earth, R_earth)
155| t_earth = OrbitalPeriod(M_earth, R_earth) / 60
156| table_line('')
157| table_line('--')
158|
159| t_spec_1 = 120
160| t_spec_2 = 180
161| t_spec_3 = 300
162| t_spec_4 = 600
163| r_spec_1 = OrbitalDistance(M_earth, t_spec_1)
164| r_spec_2 = OrbitalDistance(M_earth, t_spec_2)
165| r_spec_3 = OrbitalDistance(M_earth, t_spec_3)
166| r_spec_4 = OrbitalDistance(M_earth, t_spec_4)
167| v_spec_1 = OrbitalVelocityT(r_spec_1, t_spec_1)
168| v_spec_2 = OrbitalVelocityT(r_spec_2, t_spec_2)
169| v_spec_3 = OrbitalVelocityT(r_spec_3, t_spec_3)
170| v_spec_4 = OrbitalVelocityT(r_spec_4, t_spec_4)
171| table_line('Special 1', r=0, a=r_spec_1, t=t_spec_1/60, v=v_spec_1)
172| table_line('Special 2', r=0, a=r_spec_2, t=t_spec_2/60, v=v_spec_2)
173| table_line('Special 3', r=0, a=r_spec_3, t=t_spec_3/60, v=v_spec_3)
174| table_line('Special 4', r=0, a=r_spec_4, t=t_spec_4/60, v=v_spec_4)
175|
176| table_line('--')
177| table_line('Surface', 0, t_earth, v_earth)
178|
179|
180| r_catch_1 = 3_048
181| r_catch_2 = 10_668
182| v_catch_1 = OrbitalVelocityR(M_earth, R_earth+r_catch_1)
183| v_catch_2 = OrbitalVelocityR(M_earth, R_earth+r_catch_2)
184| t_catch_1 = OrbitalPeriod(M_earth, R_earth+r_catch_1) / 60
185| t_catch_2 = OrbitalPeriod(M_earth, R_earth+r_catch_2) / 60
186| table_line('Catch (lo)', r_catch_1, t_catch_1, v_catch_1)
187| table_line('Catch (hi)', r_catch_2, t_catch_2, v_catch_2)
188|
189|
190| r_elev = 120_700
191| v_elev = OrbitalVelocityR(M_earth, R_earth+r_elev)
192| t_elev = OrbitalPeriod(M_earth, R_earth+r_elev) / 60
193| table_line('Elevator', r_elev, t_elev, v_elev)
194|
195|
196| r_leo_1 = int(2e5)
197| r_leo_2 = int(2e6)
198| v_leo_1 = OrbitalVelocityR(M_earth, R_earth+r_leo_1)
199| v_leo_2 = OrbitalVelocityR(M_earth, R_earth+r_leo_2)
200| t_leo_1 = OrbitalPeriod(M_earth, R_earth+r_leo_1) / 60
201| t_leo_2 = OrbitalPeriod(M_earth, R_earth+r_leo_2) / 60
202| table_line('LEO (lo)', r_leo_1, t_leo_1, v_leo_1)
203| table_line('LEO (hi)', r_leo_2, t_leo_2, v_leo_2)
204|
205|
206| r_geo = 35_786_000
207| v_geo = OrbitalVelocityR(M_earth, R_earth+r_geo)
208| t_geo = OrbitalPeriod(M_earth, R_earth+r_geo) / 60
209| table_line('Geosync', r_geo, t_geo, v_geo)
210|
211|
212| v_moon = OrbitalVelocityR(M_earth, D_moon)
213| t_moon = OrbitalPeriod(M_earth, D_moon) / 60
214| table_line('Moon', D_moon, t_moon, v_moon)
215|
216| Log.info()
217| return 'Done'
218|
219|
220| def do_orbit_radius_by_mass (**kwargs):
221| """CMND: Generate orbit radii given a mass."""
222| fpath = KWARG('fpath', BasePath, str, kwargs)
223| fname = KWARG('fname', 'orbits.list', str, kwargs)
224| mass = KWARG('mass', M_earth, float, kwargs)
225| period = KWARG('period', 3600, float, kwargs)
226| factor = KWARG('factor', 2, float, kwargs)
227| Log.info('orbit-radii-by-mass:: %s' % str(list(kwargs.keys())))
228| Log.debug('fpath="%s"' % fpath)
229| Log.debug('fname="%s"' % fname)
230| Log.debug('mass=%.2f' % mass)
231| Log.debug('period=%.2f' % period)
232| Log.debug('factor=%.2f' % factor)
233| Log.info()
234|
235| fn = path.join(fpath, fname) if fpath else fname
236| fo = linefile(fn)
237|
238| fo.add(f'{period/60:.1f}-minute orbit radius.')
239| fo.add()
240| for ix,f in enumerate(range(1,25)):
241| r = OrbitalDistance(mass, period)/1000
242| fo.add('%2d: m=%9.3e kg, r=%6.2f km' % (ix,mass,r))
243| mass /= factor
244| fo.add()
245| fo.save()
246|
247| Log.info()
248| return 'Done'
249|
250|
251| def do_planet_surface_orbits (**kwargs):
252| """CMND: Generate a list of planetary surface orbit periods."""
253| fpath = KWARG('fpath', BasePath, str, kwargs)
254| fname = KWARG('fname', 'planet-periods.list', str, kwargs)
255| Log.info('planet-surface-orbits:: %s' % str(list(kwargs.keys())))
256| Log.debug('fpath="%s"' % fpath)
257| Log.debug('fname="%s"' % fname)
258| Log.info()
259|
260| def do_planet (name):
261| m = Planets[name]['mass']['kg']
262| r = Planets[name]['radius']['km']['mean'] * 1000
263| p = OrbitalPeriod(m, r) / 60
264| v = OrbitalVelocityR(m, r) / 1000
265| Log.info(f'{name:<9s} {p:6.2f} mins @ {v:5.2f} km/s [{m:.2e} kg @ {r/1000:8.2f} km]')
266| return (p, v, m, r)
267|
268| period,velocity,mass,radius = do_planet('mercury')
269| period,velocity,mass,radius = do_planet('venus')
270| period,velocity,mass,radius = do_planet('earth')
271| period,velocity,mass,radius = do_planet('moon')
272| period,velocity,mass,radius = do_planet('mars')
273| period,velocity,mass,radius = do_planet('jupiter')
274| period,velocity,mass,radius = do_planet('saturn')
275| period,velocity,mass,radius = do_planet('uranus')
276| period,velocity,mass,radius = do_planet('neptune')
277| period,velocity,mass,radius = do_planet('pluto')
278|
279| Log.info()
280| return 'Done'
281|
282|
283| def do_orbit_with_period_and_mass (**kwargs):
284| """CMND: Calculate the radius and velocity given mass and period."""
285| fpath = KWARG('fpath', BasePath, str, kwargs)
286| fname = KWARG('fname', 'orbits.out', str, kwargs)
287| mass = KWARG('mass', 100.0, float, kwargs)
288| period = KWARG('period', 24*3600, float, kwargs)
289| Log.info('mass-and-period:: %s' % str(list(kwargs.keys())))
290| Log.debug('fpath="%s"' % fpath)
291| Log.debug('fname="%s"' % fname)
292| Log.debug('mass=%.2f' % mass)
293| Log.debug('period=%.2f' % period)
294| Log.info()
295|
296| radius = OrbitalDistance(mass, period)
297| velocity = OrbitalVelocityT(radius, period) / 1000
298|
299| Log.info(f'{period/60:.1f} min orbit around a {mass:.2e} kg body:')
300| Log.info(f'{radius/1000:.3f} km @ {velocity:.3f} km/s')
301|
302| Log.info()
303| return 'Done'
304|
305|
306| def do_test (**kwargs):
307| """CMND: Do a test."""
308| fpath = KWARG('fpath', BasePath, str, kwargs)
309| fname = KWARG('fname', 'orbits.out', str, kwargs)
310| Log.info('test:: %s' % str(list(kwargs.keys())))
311| Log.debug('fpath="%s"' % fpath)
312| Log.debug('fname="%s"' % fname)
313| Log.info()
314|
315|
316| for ix,r in [(n, R_sun*(n+1)) for n in range(10)]:
317| v = OrbitalVelocityR(M_sun, r)
318| rs = f'{int(r/1000):,}'
319| Log.info(f'{ix+1:2d}: {rs:>9s} km {v/1000:.1f} m/s {int(mps2MPH(v)):,} MPH')
320| Log.info()
321|
322|
323| d = Planets['mercury']['orbit']['distance']['km']['min'] * 1000
324| v = OrbitalVelocityR(M_sun, d)
325| rs = f'{int(d/1000):,}'
326| Log.info(f'Mer: {rs:>9s} km {v/1000:.1f} m/s {int(mps2MPH(v)):,} MPH')
327|
328| Log.info()
329| return 'Done'
330|
331| def dispatch (cmd, **kwargs):
332| """Dispatch to a module function based on CMD."""
333| Log.info('CMD: %s' % cmd)
334| for kw in kwargs:
335| Log.debug('argument[%s]: %s' % (kw,kwargs[kw]))
336| Log.info()
337| if cmd == '1': return do_list_orbital_parameters(**kwargs)
338| if cmd == '2': return do_orbit_radius_by_mass(**kwargs)
339| if cmd == '3': return do_planet_surface_orbits(**kwargs)
340| if cmd == '4': return do_orbit_with_period_and_mass(**kwargs)
341| if cmd == 'test': return do_test(**kwargs)
342| return 'Nothing to do!'
343|
344| if __name__ == '__main__':
345| print('autorun: %s' % argv[0])
346| Log.start(path.join(BasePath,'orbits.log'))
347| Log.level(info())
348| Log.info(argv[0])
349| Log.info()
350| cmd = argv[1] if 1 < len(argv) else CMND
351| kwargs = ARGS
352| for a in argv[2:]:
353| nv = a.split('=', 1)
354| kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
355| Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
356| try:
357| print('CMD: %s' % cmd)
358| print()
359| obj = dispatch(cmd, **kwargs)
360| print()
361| print(obj)
362| Log.info()
363| Log.info(obj)
364| except:
365| etype, evalue, tb = exc_info()
366| ts = extract_tb(tb)
367| Log.error()
368| Log.error('%s: %s' % (etype.__name__,str(evalue)))
369| Log.error()
370| for t in ts[-5:]:
371| Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
372| Log.error(' %s' % t[3])
373| Log.error()
374| raise
375| finally:
376| Log.end()
377|
378| '''eof'''
379|