{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5bef2913-4855-471d-9594-5ec45c029048",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length: 143.2 mm\n",
      "Freq:   2.822 Hz\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "from pint import UnitRegistry\n",
    "unit = UnitRegistry()\n",
    "unit.formatter.default_format = \"~\"\n",
    "\n",
    "\n",
    "# Parameters\n",
    "spring_constant = 1.1 * 4 * unit.N / unit.mm\n",
    "spring_length_resting = 112 * unit.mm\n",
    "\n",
    "weight_total = 14 * unit.kg\n",
    "\n",
    "dampening = 1 * unit.N / (unit.m / unit.s)\n",
    "\n",
    "def spring_length_at(weight):\n",
    "    return (weight * unit.standard_gravity / spring_constant + spring_length_resting).to(unit.mm)\n",
    "\n",
    "def resonant_freq_at(weight):\n",
    "    return (1 / (2 * math.pi) * np.sqrt(spring_constant / weight)).to(unit.Hz)\n",
    "\n",
    "spring_length = spring_length_at(weight_total)\n",
    "f0 = resonant_freq_at(weight_total)\n",
    "\n",
    "print(f\"Length: {spring_length:~.1f}\")\n",
    "print(f\"Freq:   {f0:~.3f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f21d84d9-c7a1-4fd2-aa32-136f04db32e5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.00201455741006345"
      ],
      "text/latex": [
       "$0.00201455741006345$"
      ],
      "text/plain": [
       "<Quantity(0.00201455741, 'dimensionless')>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def lehr_dampening_factor(d, k, m):\n",
    "    return d / (2 * np.sqrt(m * k))\n",
    "\n",
    "lehr_dampening = lehr_dampening_factor(dampening, spring_constant, weight_total)\n",
    "lehr_dampening.ito_reduced_units()\n",
    "lehr_dampening"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b71de7db-a7fb-4d3a-9dd2-de8e107700a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3.13/site-packages/matplotlib/cbook.py:1355: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.\n",
      "  return np.asarray(x, float)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36660 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def amplitude_ratio(lehr, f0, f):\n",
    "    eta = f / f0\n",
    "    return 1 / np.sqrt((1 - eta**2)**2 + (2 * eta * lehr)**2)\n",
    "\n",
    "f_in = np.geomspace(0.5, 90, 100) * unit.Hz\n",
    "ratio = amplitude_ratio(lehr_dampening, f0, f_in)\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.plot(f_in, ratio)\n",
    "\n",
    "def transmissibility_plot_setup(plt):\n",
    "    plt.xscale('log')\n",
    "    xticks = [1, 3, 10, 50]\n",
    "    plt.xticks(xticks, [f\"{t} Hz\" for t in xticks], minor=False)\n",
    "    plt.xlabel(\"Excitation Frequency\")\n",
    "    \n",
    "    plt.ylim(0.001, 10)\n",
    "    plt.yscale('log')\n",
    "    plt.ylabel(\"Transmissibility Ratio\")\n",
    "    \n",
    "    plt.grid(True, \"both\")\n",
    "\n",
    "transmissibility_plot_setup(plt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "fea96fd4-46e1-4f07-8fb4-25ccc0ee82a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3.13/site-packages/matplotlib/cbook.py:1355: UnitStrippedWarning: The unit of the quantity is stripped when downcasting to ndarray.\n",
      "  return np.asarray(x, float)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 93392 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def ratio_for_dkm(d, k, m, f_in):\n",
    "    f0 = (1 / (2 * math.pi) * np.sqrt(k / m)).to(unit.Hz)\n",
    "    lehr = lehr_dampening_factor(d, k, m)\n",
    "    return amplitude_ratio(lehr, f0, f_in)\n",
    "\n",
    "d = dampening\n",
    "k = [\n",
    "    spring_constant,\n",
    "    spring_constant / 4,\n",
    "]\n",
    "m = [\n",
    "    weight_total,\n",
    "    24 * unit.kg,\n",
    "]\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "for k_ in k:\n",
    "    m_ = m[0]\n",
    "    plt.plot(f_in, ratio_for_dkm(d, k_, m_, f_in), label=f\"{k_:~.1f}, {m_:~.1f}\")\n",
    "for m_ in m[1:]:\n",
    "    k_ = k[0]\n",
    "    plt.plot(f_in, ratio_for_dkm(d, k_, m_, f_in), label=f\"{k_:~.1f}, {m_:~.1f}\")\n",
    "\n",
    "ref_thorlabs_f = [3, 4, 4.42, 5, 6,   7,   8,   9,    10,   40]\n",
    "ref_thorlabs_r = [2, 5, 15.0, 4, 1.5, 0.7, 0.5, 0.35, 0.28, 0.018]\n",
    "plt.plot(ref_thorlabs_f, ref_thorlabs_r, label=\"Thorlabs PTP702 (Passive)\")\n",
    "\n",
    "ref_thorlabs_f = [1, 1.35, 2,   3,   5,   9,    20,   27,     30]\n",
    "ref_thorlabs_r = [2, 3,    0.9, 0.3, 0.1, 0.02, 0.009, 0.007, 0.0023]\n",
    "plt.plot(ref_thorlabs_f, ref_thorlabs_r, label=\"Thorlabs PTS601 (Active)\")\n",
    "    \n",
    "plt.legend(loc=\"upper right\")\n",
    "transmissibility_plot_setup(plt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5b06973-5038-4598-aaf4-73b55866ba75",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
