.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples\linear_network.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_linear_network.py: Simple linear Network ====================== This example builds up a simple linear network using Capacitors and Resistors with random resistances and capacities, wrapped up in BoundaryConditions. At the end, it plots the result over the time for each Capacitor. .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python # sphinx_gallery_tags = ["network","solving","build"] # sphinx_gallery_thumbnail_path = "_static/examples/linear_rc_network.svg" import numpy as np from pyrc import Capacitor, Resistor, BoundaryCondition, RCNetwork from pyrc.visualization.plot import LinePlot .. GENERATED FROM PYTHON SOURCE LINES 23-25 Create functions for random initialized Capacitors and Resistors ----------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-45 .. code-block:: Python rng = np.random.default_rng(42) def random_value(): return rng.uniform(low=5, high=20) def random_capacitor() -> Capacitor: return Capacitor( capacity=random_value(), temperature=random_value(), ) def random_resistor() -> Resistor: return Resistor( resistance=random_value(), ) .. GENERATED FROM PYTHON SOURCE LINES 46-49 Create the boundary conditions ------------------------------- One of it is warm, one cold. .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: Python bc_warm = BoundaryCondition(30) bc_cold = BoundaryCondition(-4) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Create the network by adding all building blocks and connect them ------------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 56-68 .. code-block:: Python capacitors: list[Capacitor] = [random_capacitor()] resistors: list[Resistor] = [random_resistor()] # Connect first capacitor to boundary condition resistors[-1].double_connect(bc_warm, capacitors[-1]) for n in range(10 - 1): resistors.append(random_resistor()) capacitors.append(random_capacitor()) resistors[-1].double_connect(capacitors[-2], capacitors[-1]) .. GENERATED FROM PYTHON SOURCE LINES 69-70 Connect the boundary condition using a new Resistor .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python resistors.append(random_resistor()) resistors[-1].double_connect(capacitors[-1], bc_cold) .. GENERATED FROM PYTHON SOURCE LINES 74-78 Add the objects (Capacitors, Resistors, BCs) to an RCNetwork object -------------------------------------------------------------------- The RCNetwork class is used to solve the RC network. It just needs all the building parts in its RCObject. Also, we want a resolution of 1 second so we set the settings accordingly. .. GENERATED FROM PYTHON SOURCE LINES 78-82 .. code-block:: Python network = RCNetwork(load_from_pickle=False, load_solution=False) network.rc_objects.set_lists(capacitors=capacitors, resistors=resistors, boundaries=[bc_warm, bc_cold]) network.settings.save_all_x_seconds = 1 .. GENERATED FROM PYTHON SOURCE LINES 83-85 Solve network for the first 30 Minutes -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: Python network.solve_network(t_span=(0, 1800), print_progress=True) .. GENERATED FROM PYTHON SOURCE LINES 88-92 Print solution --------------- It uses the own LinePlot class and creates LaTeX-Labels for each Capacitor. The y-values must be transposed because we are not plotting over time but over capacitors. .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: Python plot = LinePlot(x=network.rc_solution.t, ys=network.rc_solution.y.T, labels=[f"$C_{{{c.id}}}$" for c in capacitors]) plot.plot() plot.show_legend() plot.show() .. _sphx_glr_download_auto_examples_linear_network.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: linear_network.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: linear_network.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: linear_network.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_