{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0a830669",
   "metadata": {},
   "source": [
    "# When can a force pulse be replaced by its impulse?\n",
    "\n",
    "Keep the total impulse fixed and increase contact duration. Predict whether the free vibration after release stays constant. Compare the Fourier integral with a direct quadrature of the forcing history."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "229820cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "mass,frequency=2.,8.    # kg, Hz\n",
    "impulse= .6            # N s\n",
    "duration=.050          # s\n",
    "assert min(mass,frequency,impulse,duration)>0\n",
    "omega=2*np.pi*frequency"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d1b715a",
   "metadata": {},
   "source": [
    "## Project the pulse on the free oscillator\n",
    "Use two shifted sinc functions to evaluate the normalised half-sine pulse transform without a removable singularity. NumPy sinc uses sin(pi x)/(pi x)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fc41cce",
   "metadata": {},
   "outputs": [],
   "source": [
    "def ratio(r):\n",
    "    return np.pi/4*np.abs(np.sinc((r-np.pi)/(2*np.pi))+np.sinc((r+np.pi)/(2*np.pi)))\n",
    "\n",
    "u=np.linspace(0,1,801)\n",
    "weights=np.pi/2*np.sin(np.pi*u)\n",
    "r=omega*duration\n",
    "q=weights*np.exp(-1j*r*u)\n",
    "integrated=np.sum(np.diff(u)*(q[:-1]+q[1:])/2)\n",
    "assert abs(abs(integrated)-ratio(r))<4e-6\n",
    "assert np.isclose(ratio(0),1) and np.isclose(ratio(np.pi),np.pi/4)\n",
    "amplitude=impulse/(mass*omega)*ratio(r)\n",
    "print(f\"Pulse / natural period {frequency*duration:.3f}; remaining amplitude {1000*amplitude:.3f} mm\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "328f03c1",
   "metadata": {},
   "source": [
    "## Read the shape through its residual motion\n",
    "At the same impulse, a long pulse is weaker and excites less residual motion over many duration ranges. The first exact cancellation occurs at one and a half periods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d082808",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.5),layout=\"constrained\")\n",
    "for factor,style in [(1,\"-\"),(2,\"--\")]:\n",
    "    T=duration*factor\n",
    "    ax[0].plot(u*T*1000,impulse/T*weights,style,label=f\"{T*1000:.0f} ms\")\n",
    "ax[0].set(xlabel=\"Time after contact (ms)\",ylabel=\"Force (N)\",title=\"Equal force impulse\")\n",
    "cycles=np.linspace(0,3,550)\n",
    "ax[1].plot(cycles,ratio(2*np.pi*cycles),label=\"Remaining / impulsive amplitude\")\n",
    "ax[1].plot(frequency*duration,ratio(r),\"o\",label=\"Chosen duration\")\n",
    "ax[1].set(xlabel=\"Pulse duration / natural period\",ylabel=\"Residual amplitude ratio\",title=\"Free vibration after release\")\n",
    "for p in ax: p.grid(alpha=.22);p.legend(fontsize=8)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1477f50e",
   "metadata": {},
   "source": [
    "Change the duration to half a period and then one and a half periods. Why does one case leave a sizeable oscillation while the other cancels it? Explain why cancellation after release does not imply zero displacement during contact."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
