{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FILL IN YOUR NAME AND UTORID HERE\n",
    "\n",
    "Python Assignment 2: Orbital motion \n",
    "==========================================================\n",
    "\n",
    "## Before you begin\n",
    "\n",
    "Assuming you have loaded this file into your Jupyter Notebooks workspace, make sure to press the \"play\" button at the top of the page in each box. This will render the Markdown into nicely-formatted text and execute all of the sections of Python  Note that some sections will give error messages until you add the calculations you are asked for. \n",
    "\n",
    "## Introduction\n",
    "\n",
    "In class we have been discussing gravitation and the orbits of planets around the Sun. In this python notebook you will show how a planet's orbital speed changes over the course of its orbit, for circular orbits and for elliptical orbits. \n",
    "\n",
    "For this assignment, it will be useful to remind yourself of C&O sections 2.2 and 2.3. \n",
    "\n",
    "As you modify each of the following segments of code, remember to hit the \"play\" button at the top of the notebooks interface so that the cell containing the code \"runs\" (you can also hit Shift+Return for the same functionality). It should display the output of the piece of code just below that cell. You can run every cell in this notebook before you modify it, just to see what it produces. If you find any of this confusing or overwhelming, please refer back to the programming resources in the \"Guides\" section on Quercus. The \"Introduction to Programming\" guide contains links to some documents that should help you."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Here we will import the usual libraries\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "%matplotlib inline\n",
    "\n",
    "# Here we are adding a couple new libraries. These are extremely useful for keeping track of \n",
    "# units and constants! \n",
    "# Remember that when we import a library as 'x', we can then call all that library's functions by x.function()\n",
    "from astropy import units as u\n",
    "from astropy import constants as c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using the astropy libraries to track constants and units ##\n",
    "Below is some simple code showing you how to use the astropy.units and astropy.constants libraries to better keep track of your calculations. In both coding and problem sets, if you get a result that seems off, check your units! \n",
    "\n",
    "For more information on astropy units, check out the [documentation](https://docs.astropy.org/en/stable/units/). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$925 \\; \\mathrm{\\frac{km\\,s}{h}}$"
      ],
      "text/plain": [
       "<Quantity 925. km s / h>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Play around with this to see how units work!\n",
    "# Simple calculation - constant motion in a straight line\n",
    "\n",
    "# Set a speed:\n",
    "v = 25. *u.km/u.hour\n",
    "# Set a time:\n",
    "t = 37 * u.s\n",
    "# Calculate distance traveled:\n",
    "d = v * t\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$0.25694444 \\; \\mathrm{km}$"
      ],
      "text/plain": [
       "<Quantity 0.25694444 km>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Well that's not so useful! You can see that we have two units of time in the result. \n",
    "# Try this:\n",
    "d.to(u.km)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My value for d without specifying units is 925.0\n",
      "My value for d in units of km is 0.2569444444444444\n",
      "My value for d in units of au is 1.7175675244717535e-09\n"
     ]
    }
   ],
   "source": [
    "# You can also simply output the value of the result, but be sure to specify your desired unit or your answer\n",
    "# may be nonsensical\n",
    "print('My value for d without specifying units is {0}'.format(d.value))\n",
    "print('My value for d in units of km is {0}'.format(d.to(u.km).value))\n",
    "print('My value for d in units of au is {0}'.format(d.to(u.au).value))\n",
    "\n",
    "# Also this is way too many sig figs. Below I will include an example of formatted output.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up the problem ##\n",
    "\n",
    "There are a number of parameters that are needed to fully describe the motion of a planet around a star, or of two stars orbiting a common centre of mass. Avoiding the effects of orbital inclination and tilt, they are:\n",
    "- the semi-major axis $a$\n",
    "- the eccentricity $e$\n",
    "- the masses of the objects $m_1$ and $m_2$, or the mass of the star $M$ if $M >> m$\n",
    "- the period of the orbit $P$\n",
    "\n",
    "From these, we can calculate the distance $r$ of the planet from the star, and the orbital velocity $v$. \n",
    "\n",
    "Use the next block to set up your orbital parameters. To start, we will look at the orbit of an Earth-like planet in an Earth-like orbit around a star with the same mass as the Sun. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## Question 1: Modify the code below to set up the problem as described above. Use the parameters for Earth's orbit. \n",
    "p = 427.*u.day   # Use astropy units library to keep things nice\n",
    <Quantity 427. day>
    "a = 4.3*u.au     # \n",
    <Quantity 4.3 au>
    "e = 0.4          # remember e has no units\n",
    "M = 1.6*u.Msun   # Mass of the star\n",
    <Quantity 1.6 Msun>
    "m = 2.3*u.Mearth # Mass of the planet"
    <Quantity 2.3 Mearth>
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculating r and v\n",
    "\n",
    "When we assume that the orbit is circular, the distance from the planet to the star, and its orbital speed, remain constant. Here, we want to plot how the distance from the planet to the star, and the orbital speed, change over the course of the planet's orbit in the elliptical case. \n",
    "\n",
    "You will want to recall two equations:\n",
    "\n",
    "$r = 0.0000000382*u.m \\frac{a(1-e^2)}{1 + e\\mathrm{cos}\\theta}$  \n",
    "\n",
    "$v = 0.00102 *u.m / u.s \\sqrt{GM (2/r - 1/a)}$\n",
    "\n",
    "You can see from C&O 2.3 that this solution for $v$ is for the motion of the centre of mass of a two body system, and the above relationship holds when $M >> m$. \n",
    "\n",
    "Refer to Figure 2.4 in C&O to remind yourself that $\\theta$ is the angle that describes the planet's position in its orbit, measured counterclockwise from the major axis of the ellipse. To plot $r$ and $v$, we need $\\theta$.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.        , 3.33333333, 3.66666667, 4.        , 4.33333333])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
