Stellar Luminosities from the Stefan-Boltzmann Equation#
Learning Objectives
Understand how to perform operations on Python variables to carry out computations.
Understand different data types and how to convert between them when needed.
Be able to write, save, and run a basic Python script.
Enable user input to a script for dynamic calculations.
Introduction#
Stars, to zeroth order, can be considered spherically-symmetric blackbodies. Under those conditions, their luminosities can be computed via the Stefan-Boltzmann law,
where \(R\) is the radius, \(T\) is the effective temperature of the Star, and \(\sigma\) is the Stefan-Boltzmann constant, \(\sigma = 5.67 * 10^{-8} \rm \ W/m^{2}/K^{4}\).
The goal of this exercise is to implement this in Python, i.e., write a code to calculate the Luminosity (\(L\)) of a star according to the formula described above.
Basic Scripting#
Here we will create a barebones script to hold our calculation, using several variables
to aid in our computation.
Exercise 1
Create a new python script, e.g.,
SB.py
and open it in an editor.Within SB.py, define a unique variable for each of \(\pi\), \(\sigma\), \(R\), and \(T\). For now, assume \(\pi = 3.14\), \(R = 7 * 10^8\) meters and \(T = 5776\) K. You can assume the units work out (and therefore you can ignore them in Python).
Using basic mathematical operations, compute the value of \(L\) according to the formula above using the variables you just defined. Store the result in a new variable.
Hint: powers in python are implemented via the **
operator, e.g., 6**2 = 36
Exercise 2
Now that we have some code that uses the variables we’ve created to compute the new value, let’s create a nice print statement
of the result and run our script.
Using f-strings, print the result in a sentence that looks like: The Luminosity of a star with effective temperature [value of T] and radius [value of R] is [value of L].
Bonus: Can you format your f-strings such that the rounding looks nice?
Test the script by running it from the terminal. Is your value greater, less than, or equal to the Solar luminosity of \(3.8 * 10^{26}\) Watts?
Dynamic Scripting#
Let’s now improve the usability of our code. Instead of hardcoding (i.e., setting) the values of \(T\) and \(R\) within the script, we will have the script ask for these values from the user when the script is run.
Exercise 3
Use the python builtin
input()
function to allow users (i.e., you) to provide the values in the terminal at runtime. This function will parse the input as thestring
datatype, so you will need to use theint()
orfloat()
functions to turn the inputs into “numbers” that can be plugged into your formula.Implement a comparison to the Solar Luminosity within your code and store the result a new
boolean
(True/False) variable calledis_supersolar
. Then extend yourf-string
above to print whether the star is (or is not) more luminous than the Sun. The output should look like: The Luminosity of a star with effective temperature [value of T] and radius [value of R] is [value of L]. It is [value of supersolar] that this is more luminous than the Sun.
Bonus: If you know how to use an if-statement
, you could use one to simply adjust the second line of text to something more natural in either case.
Run your script, entering the values we used above, and make sure your code works. Try out different input values for \(T\) and \(R\) to see how the luminosity is affected.
For the chosen input temperature \(T\), what radius \(R\) would be needed to end up with a luminosity \(10x\) greater than that of the sun? You can find this with some trial and error.
Note
Having users input values at runtime is not the most common way to take dynamic inputs to code, but is very useful for highly “interactive” programs like this
Fun with Units (Bonus)#
In this bonus exercise, we’ll use the astropy
library to handle units more elegantly. The astropy.units
module, which we typically import as
import astropy.units as u
is very useful for handling unit conversions and checks for us. Meanwhile, the astropy.constants
library, e.g.,
import astropy.constants as ac
provides pretty much all the constants we use in physics and astronomy. You can attach units to a value by multiplying:
some_temperature = 5000 * u.K # Kelvin
where most of the unit name shorthands are what you would expect.
Exercise 4
Simplify your script by using the astropy-provided Stefan-Boltzmann constant, and attaching units of Kelvin and Meters to the input, before performing the calculation. Use the
.to()
method of a unit-carrying variable to print the final result inu.Lsun
(solar luminosities).For an added challenge, allow the user input to feature units (e.g., input
1 Lsun
). You’ll need tosplit
your input string on the space character (' '
) to separate the part which needs to be turned into a float and the part which needs to be a unit. You can assume the string for the unit is oneastropy
recognizes, but rather than multiplying byu.THING
, you’ll need to multiply byu.Quantity('string')
in order to use the input unit.
Practice with List Indexing#
Let’s imagine you have data for bright stars that looks a bit like:
star_names = ["Sirius", "Canopus", "Rigil Kentaurus", "Arcturus", "Vega", "Capella", "Rigel", "Procyon", "Achernar", "Betelgeuse", "Acrux", "Altair", "Aldebaran", "Spica", "Antares", "Pollux", "Fomalhaut", "Deneb", "Regulus", "Adhara"]
star_temperatures = [9940, 7400, 5800, 4300, 9600, 4900, 12100, 6550, 14600, 3500, 28000, 7700, 3900, 22200, 3400, 4940, 8550, 8525, 12300, 20800]
star_radii = [1.711e9, 7.8e8, 1.227e9, 2.785e9, 2.364e9, 1.192e9, 7.6e8, 7.43e8, 1.586e9, 9.52e10, 6.01e9, 1.656e9, 4.28e9, 7.739e8, 8.12e9, 1.927e9, 1.834e9, 8.48e9, 3.919e9, 4.66e9]
Exercise 5
Create a new script called star_calcs.py.
Using indexing, figure out: what the name of the tenth star in this list? Then, store the index in a variable.
Now, using the stored index, isolate the temperature of the star by accessing the appropriate element of the second list.
Repeat step 2 for the radius.
By copy-pasting your formula implementation from exercise 1 into your new script, compute the luminosity of the star of the tenth star.
Now that we’ve finished with star 10, let’s try to make it our code more generally applicable.
Have your program take an index in via input() and run the calculation for the star corresponding to that index.
(BONUS): Create a dictionary for each of the two properties for the first 5 stars above (that is, a dictionary for radius and a dictionary for temperature). The keys should be the star names.
(BONUS part 2): Now have your program take the star name as an input and print the name, temperature, radius, and computed luminosity in a nicely formatted sentence.