{ "cells": [ { "cell_type": "markdown", "id": "overhead-silly", "metadata": {}, "source": [ "# Series RLC Circuit\n", "\n", "Such a circuit is defined by the following equations:\n", "\n", "$$\\begin{align}\n", "\\text{R}&: V_R = I_R R \\\\\n", "\\text{L}&: V_L = L\\frac{dI_L}{dt} \\\\\n", "\\text{C}&: I_C = C\\frac{dV_C}{dt}\n", "\\end{align}$$\n", "\n", "Additionally, there are the Kirchhoff equations:\n", "\n", "$$I_R = I_L = I_C \\equiv I \\\\\n", "V_R + V_L + V_C = 0$$\n", "\n", "These equations may be written more succintly as\n", "\n", "$$\\begin{align}\n", "\\text{R}&: V_R = IR \\\\\n", "\\text{L}&: \\frac{dI}{dt} = - (V_R + V_C) / L \\\\\n", "\\text{C}&: \\frac{dV_C}{dt} = I / C\n", "\\end{align}$$\n", "\n", "Then the needed variables are clear: (VR, I, VC), Where 2 equations are differential and 1 is algebraic" ] }, { "cell_type": "code", "execution_count": null, "id": "sorted-strain", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from matplotlib import rc\n", "\n", "rc(\"font\", family=\"serif\", size=15)\n", "rc(\"savefig\", dpi=600)\n", "rc(\"figure\", figsize=(8, 6))\n", "rc(\"mathtext\", fontset=\"dejavuserif\")\n", "rc(\"lines\", linewidth=3)\n", "from networkx import DiGraph\n", "\n", "from stream import Aggregator, Calculation, unpacked" ] }, { "cell_type": "code", "execution_count": null, "id": "alert-franklin", "metadata": {}, "outputs": [], "source": [ "class R(Calculation):\n", " def __init__(self, R):\n", " self.R = R\n", " self.name = \"R\"\n", "\n", " @unpacked\n", " def calculate(self, VR, *, I):\n", " return VR - I * self.R\n", "\n", " @property\n", " def mass_vector(self):\n", " return (False,)\n", "\n", " @property\n", " def variables(self):\n", " return {\"VR\": 0}" ] }, { "cell_type": "code", "execution_count": null, "id": "congressional-token", "metadata": {}, "outputs": [], "source": [ "class L(Calculation):\n", " def __init__(self, L):\n", " self.L = L\n", " self.name = \"L\"\n", "\n", " @unpacked\n", " def calculate(self, I, *, VR, VC):\n", " return -(VR + VC) / self.L\n", "\n", " @property\n", " def mass_vector(self):\n", " return (True,)\n", "\n", " @property\n", " def variables(self):\n", " return {\"I\": 0}" ] }, { "cell_type": "code", "execution_count": null, "id": "amended-auction", "metadata": {}, "outputs": [], "source": [ "class C(Calculation):\n", " def __init__(self, C):\n", " self.C = C\n", " self.name = \"C\"\n", "\n", " @unpacked\n", " def calculate(self, VC, *, I):\n", " return I / self.C\n", "\n", " @property\n", " def mass_vector(self):\n", " return (True,)\n", "\n", " @property\n", " def variables(self):\n", " return {\"VC\": 0}" ] }, { "cell_type": "code", "execution_count": null, "id": "differential-triple", "metadata": {}, "outputs": [], "source": [ "r, l, c = R(1.0), L(1.0), C(1.0)\n", "\n", "g = DiGraph()\n", "g.add_edge(r, l, variables=(\"VR\",))\n", "g.add_edge(c, l, variables=(\"VC\",))\n", "g.add_edge(l, r, variables=(\"I\",))\n", "g.add_edge(l, c, variables=(\"I\",))\n", "\n", "agr = Aggregator(g)\n", "\n", "plt.figure(figsize=(12, 2))\n", "agr.draw(\n", " node_options=dict(pos={r: (0, 0), l: (1, 0), c: (2, 0)}, node_size=2000, font_size=24),\n", " edge_options=dict(label_pos=0.3, font_size=18),\n", ")\n", "plt.box()" ] }, { "cell_type": "code", "execution_count": null, "id": "fitted-partnership", "metadata": {}, "outputs": [], "source": [ "from stream.analysis.report import report\n", "\n", "report(agr)" ] }, { "cell_type": "code", "execution_count": null, "id": "moved-liberty", "metadata": {}, "outputs": [], "source": [ "y0 = np.array([1, 1.0, -1])\n", "time = np.linspace(0, 10)\n", "\n", "sol = agr.solve(y0=y0, time=time, yp0=agr.compute(y0, 0))" ] }, { "cell_type": "code", "execution_count": null, "id": "photographic-settle", "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(8, 6))\n", "plt.plot(time, sol[:, 0], label=\"VR\")\n", "plt.plot(time, sol[:, 2], label=\"VC\")\n", "plt.legend()\n", "plt.grid()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "96a64773", "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }