{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spinning Basketball\n",
    "## Lab 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Name:\n",
    "### Student ID:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Instructions:\n",
    "Skim through the entire lab before beginning.  Read the text carefully and complete the steps as indicated.  Submit your completed lab through the D2L dropbox.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start by carefully watching the following video:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import YouTubeVideo\n",
    "\n",
    "YouTubeVideo(\"QtP_bh2lMXc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The purpose of this lab is to analyze the motion of spinning ball dropped vertically from the top of Gordon Dam as shown in the video.  \n",
    "\n",
    "The initial dropping of the basket ball shows that it deviates from falling straight down.  This is probably due to the wind.  Notwithstanding this potentially important effect, the analysis in this lab is ignore the effect of the wind."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Theoretical Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The dynamics of spinning ball under the influence of both a quadratic drag force and the Magnus force have been discussed in [Lecture 5](lec5.ipynb).  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Basketballs and physics go naturally together. In 2011, [The University Physics Competition](http://www.uphysicsc.com/2011contest.html) proposed the following problem:\n",
    "\n",
    "> In the game of basketball, a player scores three points by successfully making a shot from beyond the three point line, which is 6.2 meters away from the basket in international games, such as the Olympics.  Suppose a player in an Olympic basketball game is at the three point line standing at a point making an angle 45 degrees to the principal axes of the court.   What initial ball velocities and spins will result in a successful shot from this point? \n",
    "\n",
    "A winning paper from that competition was\n",
    "\n",
    "> Team 379: Rebecca Schutzengel, Patrick Varin, & Brendan Quinlivan\n",
    "\n",
    "> Institution: F. W. Olin College of Engineering\n",
    "\n",
    "> Team Advisor: Yevgeniya V. Zastavker\n",
    "\n",
    "> [The 2011 Problem B Gold Medal Winning Paper: \"The Physics of a Three Point Shot\"](http://www.uphysicsc.com/2011-GM-B-379.PDF)\n",
    "\n",
    "Go and briefly skim their award winning paper. In particular, note their discussion of the path through air when describing their model.  Their theory and assumptions are reproduced below:\n",
    "\n",
    ">A spinning ball in the air is subject to three forces: gravity, drag and the Magnus force.\n",
    "Therefore, the motion of the ball is governed by the following\n",
    "equation:\n",
    "\n",
    "\\begin{align}\n",
    "\\frac{d^2 \\vec{r}}{dt^2} =\\vec{g} + \\frac{1}{m_{ball}}\\left( \\frac{16}{3} \\pi^2 r_{ball}^3  \\rho \\vec{\\omega} \\times \\vec{v} - \\frac{1}{2} C_d \\rho A |\\vec{v}|^2 \\hat{v} \\right)\n",
    "\\end{align}\n",
    "\n",
    "\n",
    "\n",
    "> where $\\vec{r}$, $\\vec{v}$, and $\\vec{\\omega}$ are the position, velocity, and angular\n",
    "velocity of the basketball, $\\vec{g}$ is the acceleration due to gravity,\n",
    "$m_{ball}$, $r_{ball}$ and $A$ are the mass, radius, and cross-sectional\n",
    "area of the basketball, $\\rho$ is the density of air and Cd is the\n",
    "drag coefficient of the basketball in the air which we found\n",
    "in literature to be 0.54. The first term in Equation 1\n",
    "accounts for gravity, the second term accounts for the Magnus\n",
    "force due to the ball’s spin  and the third term accounts for\n",
    "the drag force. For simplicity, the basketball was assumed\n",
    "to be spinning with a constant angular velocity throughout\n",
    "its trajectory. It was also assumed that the spin was purely\n",
    "backspin, meaning that the axis of rotation lies in the $xy$-plane\n",
    "and is perpendicular to the ball’s trajectory\n",
    "\n",
    "Also make note the table of Model Parameters found at the end of their paper. \n",
    "\n",
    "While the paper is on different physical problem, their analysis serves as a useful starting point for our own simulation of a basketball projectile."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numerical model\n",
    "\n",
    "Our goal will be to develop a numerical model to predict the trajectory of dropped basketball from an initial height $H$ and rotation rate $\\omega$.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialize parameters\n",
    "\n",
    "Starting by defining some variables for various physical parameters in the system. For each parameter need, type in a values from taken from either video or the paper. Be sure to include a comment describing the parameter and the units. Value for acceleration due to gravity and air density have already been added to show the expected format.\n",
    "\n",
    "If you require additional physical parameters, please add them here as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = 9.81 # m/s^2, accleration due to gravity\n",
    "ρ = 1.225 # kg/m^3, density of air\n",
    "H = ___\n",
    "r = ___\n",
    "m = ___\n",
    "C = ___\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Numerical method\n",
    "\n",
    "The code below is intended to solve equation of motion shown above using Euler's method. Fill in the missing parts to complete the implementation.  In order to do this, it will probably be helpful to work out the details on scrap paper first.   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate(ω=0.0, dt=1.0, tmax=30.0):\n",
    "    \"\"\"\n",
    "    Solve for the trajectory of a spinning basketball dropped from a height H spinning \n",
    "    \n",
    "    Inputs: ω is the rotation rate of the back spin on the ball\n",
    "            dt is the time step for the simulation\n",
    "            tmax is the length in time of the simulation\n",
    "            \n",
    "    Return t, y, x giving the trajectory of the basketball.\n",
    "    \"\"\"\n",
    "    \n",
    "    # calculate the required number of time steps\n",
    "    N = np.round(tmax/dt)\n",
    "    \n",
    "    # allocated arrays\n",
    "    t = np.zeros(N)\n",
    "    y = np.zeros(N)\n",
    "    x = np.zeros(N)\n",
    "    vx = np.zeros(N)\n",
    "    vy = np.zeros(N)\n",
    "    \n",
    "    # intialize variables\n",
    "    t[0] = ___\n",
    "    y[0] = ___\n",
    "    x[0] = ___\n",
    "    vx[0] = ___\n",
    "    vy[0] = ___\n",
    "    \n",
    "    # apply Euler's method\n",
    "    for i in range(N-1):\n",
    "\n",
    "        # update the positions\n",
    "        x[i+1] = x[i] + ___\n",
    "        y[i+1] = y[i] + ___\n",
    "        \n",
    "        # calculate the velocity magnitude\n",
    "        v_mag = np.sqrt(vx[i]**2 + vy[i]**2)\n",
    "        \n",
    "        # compute air drag\n",
    "        Fdrag_x = ___\n",
    "        Fdrag_y = ___\n",
    "        \n",
    "        # compute Magnus force\n",
    "        Fmagnus_x = ___\n",
    "        Fmagnus_y = ___\n",
    "        \n",
    "        # update velocities\n",
    "        vx[i+1] = vx[i] + ( ___ ) * dt\n",
    "        vy[i+1] = vy[i] + ( ___ ) * dt\n",
    "        \n",
    "        # when the ball hits the ground/water, assume it stops immediately.\n",
    "        if y[i+1] < 0:\n",
    "            vx[i+1] = 0\n",
    "            vy[i+1] = 0\n",
    "    \n",
    "    return t, y, x, vx, vy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot(t, y, x, label=''):\n",
    "    plt.plot(x, y, label='')\n",
    "    plt.xlabel('x (m)')\n",
    "    plt.ylable('y (m)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(figsize=(8,6))\n",
    "t, y, x, vx, vy = calculate(ω=0)\n",
    "plot(t, y, x, label='No rotation')\n",
    "plt.title('Trajectory of a Basketball dropped off of Gordon Dam')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Verify that we have made a reasonable choice for the time step `dt` and, if needed, adjust the time step accordingly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Estimating $\\omega$\n",
    "At the very end of the video, the presenter estimates the horizontal distance traveled by the basketball.  Let's call this distance $L$ and define it to be the distance until the first bounce of the basketball off the water.  We may assume the water is at a height of $y = 0$ m. Fill in the estimated value of $L$ below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "L = ___ # m, horizontal distance travelled by basketball before first bounce"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One parameter that we do not know is the rotation rate of the basketball.  Notice that the `calculate()` function has an argument `ω` that can be used to pass in a value for this rotation rate.\n",
    "\n",
    "Using the `calculate()` and `plot()` functions defined above, try and estimate the experimental value of $\\omega$ that best matches the observed distance traveled.\n",
    "\n",
    "There are a few approaches you could take to solve this part of the lab.  Talk with the intructor or the TA if you need a suggestion."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Impact velocity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just before the basketball hits the water, use your numerical model to estimate the impact velocity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding the bounce"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's go back and improve the `calculate()` function so that instead of the basketball immediately coming to rest upon impact, it *bounces* off the water as shown in the video. \n",
    "\n",
    "The bounce of the basketball on the water is not perfectly elastic but there loss of energy and momentum (due to the water waves, the acoustic wave, material properties of the ball, etc.).  As a parametrization, we could make the assumption that the velocity upon impact (which occurs if $y<0$) is modified as follows\n",
    "\n",
    "\\begin{align}\n",
    "v_x &= \\;\\; \\epsilon v_x \\\\\n",
    "v_y &= - \\epsilon v_y \\\\\n",
    "\\end{align}\n",
    "for some unknown parameter $\\epsilon$ with $0 \\leq \\epsilon \\leq 1$.  The case $\\epsilon = 1$ would be for a perfectly elastic collision again a rigid horizontal surface.\n",
    "\n",
    "Go back and modify (only slightly) the `calculate()` function to include this effect. You'll will need to choose a value of $\\epsilon$ (see the observations from the video).  Any plots or analysis you made above should not be affected by this change but go back and recompute the entire notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Discussion\n",
    "This lab is based on the assumption that the only forces on the ball were air drag, gravity and the Magnus force.  Based on your analysis of the dynamics in the lab as compared to the observation of the trajectory in the video, do you think that our model reasonably captures the motion of the Magnus force?  If not, what other physical aspects might we want to include in our model?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*your answer here...*"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}