{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d9214db7069245fd93626d36c9da004f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='Ls_freq_dep', options=(('constant Ls', False), ('frequency dependn…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "from ipywidgets import interact, FloatSlider\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "# frequncy axis\n",
    "f = np.logspace(np.log10(100), np.log10(10e3), 1000)\n",
    "omega = 2*np.pi*f\n",
    "\n",
    "# primary winding - voice coil\n",
    "Re = 8\n",
    "Le = .5e-3\n",
    "\n",
    "def eddy_currents_interactive_func(Ls_freq_dep, coupling_factor, Rs, Ls):\n",
    "    if Ls_freq_dep:\n",
    "        Ls *= 1 / np.sqrt(omega) # example of frequency dependnet Ls (eddy currents in iron) \n",
    "    else:\n",
    "        Ls *= 1e-3 # [mH to H] # example of constant Ls (transformer)\n",
    "    \n",
    "    # secondary winding - eddy currents: Rs, Ls\n",
    "    \n",
    "    # mutual inductance\n",
    "    M = coupling_factor*np.sqrt(Le*Ls)\n",
    "    \n",
    "    # input impedance\n",
    "    Ze = Re + 1j*omega*Le + (omega*M)**2 / (Rs + 1j*omega*Ls)\n",
    "    \n",
    "    \n",
    "    fig, ax = plt.subplots(2, figsize=(8,5))\n",
    "\n",
    "    ax[0].semilogx(f, np.real(Ze))\n",
    "    ax[0].set(ylabel = 'Resistance [$\\Omega$]')\n",
    "    ax[0].set(xlabel ='Frequency')\n",
    "\n",
    "    ax[1].semilogx(f, 1000*np.imag(Ze)/omega)\n",
    "    ax[1].set(ylabel = 'Inductance [mH]')\n",
    "    ax[1].set(xlabel = 'Frequency')\n",
    "    \n",
    "\n",
    "interact(eddy_currents_interactive_func, \\\n",
    "         Ls_freq_dep=[('constant Ls', False), ('frequency dependnet Ls', True)], \\\n",
    "         coupling_factor=FloatSlider(min=0, max=1, step=0.01, value=0.9, continuous_update=False), \\\n",
    "         Rs=FloatSlider(min=1,max=100,step=0.01, value=8, continuous_update=False), \\\n",
    "         Ls=FloatSlider(min=0.01,max=10,step=0.01, value=0.5, continuous_update=False), \\\n",
    "         );\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
