{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Luminosity Calculator Interactive\n", "\n", "This interactive figure lets you investigate how the temperature and radius of a star affect the amount of energy it puts out every second, which is the star's **luminosity**.\n", "\n", "Luminosity has metric units of Watts (e.g. a 100 Watt light bulb is converting electrical energy into light and heat at a rate of 100 Watts). However, since stars are very luminous (at least compared to most things on a \"human\" scale, in astronomy typically use units of solar luminosity, $L_\\odot$, where the Sun's luminosity is $1 L_\\odot = 3.83\\times10^{26}$ Watts!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The figure below shows a model star. Use the sliders to change the radius and the temperature of the star. Using our Sun as a baseline, the sliders take the radius in Solar Radii and the temperature in Solar Temperatures. The luminosity is reported in the lower right in both Watts and Solar Luminosities and the temperature is translated to units of Kelvin (for a baseline, room temperature is approximately 295 K).\n", "\n", "Use this interactive to explore the following questions:\n", "\n", "1. What changes the luminosity more, doubling the radius or doubling the temperature? \n", "2. What color are the hottest stars? The coolest stars?\n", "3. If a star is blue in color and large in radius, what can you say about its luminosity compared to a smaller star of the same color? \n", "4. If a star is blue in color and large in radius, what can you say about its luminosity compared to a red star of the same size?\n", "5. If two stars of the same size but different colors orbit each other, which stars will be more luminous?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Author: Andrew Louwagie Gordon\n", "# Date Created: 22May2018\n", "# Last Modified: 22Jun2018 (tweaked by Juan Cabanela)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import Block\n", "# Import the necessary packages\n", "from IPython.display import display\n", "import numpy as np\n", "import ipywidgets as widgets\n", "import bqplot as bq\n", "import pythreejs as p3j\n", "import tempNcolor as tc\n", "import number_formatting as nf\n", "import starlib as star" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Function Definitions Block\n", "def Star_Temp(T):\n", " '''\n", " This function calculates the temperature of the star in Kelvin.\n", " '''\n", " global T_Sun\n", " \n", " temp = T * T_Sun\n", " temp = round(temp, -2) # Round the temperature to the nearest 100 K\n", " return int(temp)\n", "\n", "\n", "def L_Ratio(t, r):\n", " '''\n", " This function calculates the ratio of luminosities for the star based on temperature and radius.\n", " '''\n", " lum = (r ** 2.0) * (t ** 4.0) # Luminosity calculation in L/L_sun\n", " \n", " return nf.SigFig(lum, 2)\n", "\n", "\n", "def UpdateWidgets(change=None): \n", " '''\n", " This function continuously updates the widgets that display information.\n", " '''\n", " \n", " # Get the luminosity ratio for this star and display it\n", " get_l_ratio = L_Ratio(Temp.value, Rad.value)\n", " L_Ratio_report.value = str(get_l_ratio)\n", "\n", " # Compute the luminosity in Watts and display it.\n", " Luminosity = float(get_l_ratio) * L_Sun\n", " latex = nf.exp2LaTeX(Luminosity,3)\n", " Luminosity_report.value = '{}'.format(latex)\n", " \n", " # Set the temperature of this star and display it\n", " t_star = Star_Temp(Temp.value)\n", " t_star_report.value = str(t_star)\n", "\n", "def UpdateStar(change=None):\n", " '''\n", " This function continuously updates the color and radius (really scale) of the star.\n", " '''\n", " global init_r, star_sphere\n", " \n", " # Get temperature in K and assign associated hexcolor\n", " t_star = Star_Temp(Temp.value)\n", " hex_color = tc.rgb2hex(tc.temp2rgb(t_star))\n", " \n", " # Set the color of the star image\n", " star.StarMeshColor(star_sphere, hex_color)\n", " \n", " # Set the scale of the star image\n", " scale_dim = Rad.value/init_r\n", " star_sphere.scale = (scale_dim, scale_dim, scale_dim)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define constants \n", "L_Sun = star.L_Sun # Solar luminosity in Watts\n", "T_Sun = star.Te_Sun # Solar temperature in Kelvin\n", "t_star = T_Sun # Define variable to be updated later\n", "Luminosity = 1 # Define variable to be updated later\n", "get_l_ratio = 1 # Define variable to be updated later\n", "\n", "# Make a list from the number2LaTeX converter being used\n", "latex = nf.exp2LaTeX(Luminosity) \n", "\n", "# Define initial conditions to be Sun-like\n", "init_temp = 1\n", "init_rad = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Widgets Definitions Block\n", "\n", "# Radius slider in units of R/R_Sun\n", "Rad = widgets.FloatSlider(\n", " min=0.2, \n", " value=1.0,\n", " max=15, \n", " step=0.1, \n", " disabled=False, \n", " continuous_update=True, \n", " orientation='horizontal', \n", " readout=True, \n", " readout_format='.1f',\n", " layout=widgets.Layout(border='none', width='200px')\n", ")\n", "\n", "# Temperature slider in units of T/T_Sun\n", "Temp = widgets.FloatSlider(\n", " min=0.5,\n", " value=1.0,\n", " max=7.0,\n", " step=0.1,\n", " disabled=False,\n", " continuous_update=True,\n", " orientation='horizontal',\n", " readout=True,\n", " readout_format='.1f',\n", " layout=widgets.Layout(border='none', width='200px')\n", ")\n", "\n", "# Widget to report updated temperature in Kelvin\n", "t_star_report = widgets.Text(\n", " value = str(int(t_star)),\n", " readout_format='.0f',\n", " placeholder = 'Type something',\n", " disabled = True \n", ")\n", "\n", "# Widget to report updated luminosity in L/L_sun\n", "L_Ratio_report = widgets.Text(\n", " value = str(get_l_ratio),\n", " placeholder = 'Type something',\n", " disabled = True \n", ")\n", "\n", "# Widget to report updated luminosity in Watts \n", "Luminosity_report = widgets.HTML(\n", " value = '{}'.format(latex),\n", " placeholder = 'Type something',\n", " disabled = True \n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Reset to initial values\n", "Temp.value = init_temp\n", "Rad.value = init_rad\n", "\n", "# Set viewer size\n", "view_width = 300\n", "view_height = 300\n", "\n", "# Get the initial temperature for the star\n", "t_star = Star_Temp(Temp.value) \n", "t_star_report.value = str(t_star)\n", "\n", "# Compute the luminosity in Watts and display it.\n", "latex = nf.exp2LaTeX(float(get_l_ratio) * L_Sun,3)\n", "Luminosity_report.value = '{}'.format(latex)\n", "\n", "# Set scale factor for radius (approximately 10 pixels per solar radius)\n", "scale_factor = 1\n", "\n", "# Set initial parameters based on stellar parameters\n", "r1 = scale_factor*Rad.value\n", "\n", "# Save initial radius to scale all other radii to this\n", "init_r = r1\n", "\n", "# set the scale\n", "scale1 = (r1/init_r, r1/init_r, r1/init_r)\n", "\n", "# Create a stellar image sphere (including a copy that represents the Sun, since it will use initial values)\n", "star_sphere = star.StarMesh(t_star, r1, scale1, [0, 0, 0])\n", "sun_sphere = star.StarMesh(t_star, r1, scale1, [0, 18, 0])\n", "\n", "# Makes the scene environment, not sure how the background works yet\n", "scene2 = p3j.Scene(children=[star_sphere, sun_sphere], background='black')\n", "\n", "# Creates the camera so you can see stuff. Place the cemera just above the x-axis and orient camera so up\n", "# is along y-axis.\n", "starcam = p3j.PerspectiveCamera(position=[45, 0, 0], up=[0, 0, 1])\n", "\n", "# Makes a controller to use for the \n", "controller = p3j.OrbitControls(controlling=starcam, enableRotate=False, enableZoom=False)\n", "\n", "# creates the object that gets displayed to the screen\n", "renderer2 = p3j.Renderer(camera=starcam, \n", " scene=scene2, \n", " controls=[controller],\n", " width=view_width, height=view_height)\n", "\n", "# Use the UpdateStar function to continuously update the star in the plot\n", "Temp.observe(UpdateStar, names=['value'])\n", "Rad.observe(UpdateStar, names=['value'])\n", "\n", "# Use the UpdateWidgets function to continuously update the calculated values in the display widgets on the bottom\n", "Temp.observe(UpdateWidgets, names=['value'])\n", "Rad.observe(UpdateWidgets, names=['value'])\n", " \n", "# Define the layout for the final widget to make it presentable\n", "box_layout = widgets.Layout(align_items='center', justify_content = 'flex-end', border='none', width='800px')\n", "\n", "# Arrange and display all the widgets in a presentable manner\n", "top_box = widgets.VBox([widgets.HTML (\"

## Model Star

\"), renderer2, \n", " widgets.HTML (\"

Model Star in center, Sun shown to right for comparison.

\")], \n", " layout = box_layout)\n", "Rad_label = widgets.HTML('Radius (R):', \n", " layout=widgets.Layout(align = 'right', width='150px'))\n", "rad_slide = widgets.HBox([Rad_label, Rad],\n", " layout=widgets.Layout(border='none', width='350px'))\n", "Temp_Label = widgets.HTML('Temperature (T):', \n", " layout=widgets.Layout(align = 'right', width='150px'))\n", "temp_slide = widgets.HBox([Temp_Label, Temp],\n", " layout=widgets.Layout(border='none', width='350px'))\n", "temp_disp = widgets.HBox([widgets.Label('Temperature (K):'), t_star_report],\n", " layout=widgets.Layout(border='none'))\n", "temp_disp.children.layout.width = '150px'\n", "temp_disp.children.layout.width = '100px'\n", "lratio_disp = widgets.HBox([widgets.HTML('Luminosity (L):'), L_Ratio_report],\n", " layout=widgets.Layout(border='none'))\n", "lratio_disp.children.layout.width = '150px'\n", "lratio_disp.children.layout.width = '100px'\n", "lum_disp = widgets.HBox([widgets.Label('Luminosity (W):'), Luminosity_report],\n", " layout=widgets.Layout(border='none'))\n", "lum_disp.children.layout.width = '150px'\n", "lum_disp.children.layout.width = '100px'\n", "\n", "bottom_left = widgets.VBox([temp_slide, rad_slide])\n", "bottom_right = widgets.VBox([temp_disp, lratio_disp, lum_disp])\n", "bottom = widgets.HBox([bottom_left, bottom_right])\n", "the_box = widgets.VBox([top_box, bottom], layout = box_layout)\n", "display(the_box)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }