astropy
Astropy is an open source collection of tools for astronomers written in Python. One of astropy's most useful features for using Python as a calculator is its units module. This module lets you assign units to variables and understands how they propagate through arithmetic.
a) Print the masses:
In [11]:
import numpy as np
# This is how we'll access astropy's units module:
import astropy.units as u
# astropy stores fundamental physical constants like
# the mass of the sun M_sun
from astropy.constants import M_sun
masses = u.Quantity([4.31e6*M_sun, 1*M_sun, 1.4*M_sun])
masses
Out[11]:
b) Retrieve constants from astropy's constants
module
In [23]:
# Newton's gravitational constant G,
# speed of light c, radius of sun R_sun
from astropy.constants import c, G, R_sun
# Convert the masses to units of grams using the `to` method
masses.to(u.gram)
Out[23]:
c) Compute $r_s$, convert to cm
In [20]:
r_s = 2*G*masses/c**2
print "r_s in centimeters: {0}".format(r_s.to(u.cm))
d) $r_s$ in units of solar radii:
In [19]:
print "r_s in solar radii: {0}".format(r_s/R_sun)
e) $r_s$ in units of AU
In [25]:
print "r_s in AU: {0}".format(r_s.to(u.AU))
f) Compare $r_s$ of Sag A* to orbits of solar system planets:
In [69]:
%matplotlib inline
import matplotlib.pyplot as plt
planet_names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
semimajor_axes = u.Quantity([0.38, 0.72, 1, 1.5, 5.2, 9.5, 19.1, 30.0], unit=u.AU)
print "Any planets interior to r_s?: {0}".format(any(r_s[0] > semimajor_axes))
fig, ax = plt.subplots()
ax.bar(range(len(semimajor_axes)), semimajor_axes.to(u.AU).value,
log=True, color='k')
ax.set_xticklabels(planet_names, ha='left')
ax.axhline(r_s[0].to(u.AU).value, color='r', label='$r_s$')
ax.legend()
ax.set_ylabel('Semimajor Axis [AU]');
g) How much bigger is neutron star radius than $r_s$?
In [73]:
radius_neutron_star = 10*u.km
print("how much bigger is neutron star's radius?: {0:.2f}x bigger"
.format(float(radius_neutron_star/r_s[2])))
In [47]:
planet_names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto']
semimajor_axes = u.Quantity([0.38, 0.72, 1, 1.5, 5.2, 9.5, 19.1, 30.0, 40], unit=u.AU)
semimajor_axes.to(u.cm)
Out[47]:
b-c) Orbital period, in years
Use numpy's np.pi
variable to get a high-precision value for $\pi$
In [64]:
T = (4 * np.pi**2 * semimajor_axes**3 / (G*M_sun))**0.5
T.to(u.year)
Out[64]:
d) Light travel time in hours, given
$$\Delta t = \frac{\Delta x}{c}$$
In [65]:
(semimajor_axes/c).to(u.hour)
Out[65]:
In [ ]: