Debian Codegraph @

doc/source/tutorials/Circuit.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "raw",
5 "metadata": {},
6 "source": [
7 ".. _circuit::\n",
8 "\n",
9 "|\n",
10 "|\n",
11 "\n",
12 "Download This Notebook: :download:`Circuit.ipynb`"
13 ]
14 },
15 {
16 "cell_type": "markdown",
17 "metadata": {},
18 "source": [
19 "# Circuits\n",
20 "## Introduction\n",
21 "\n",
22 "The [Circuit class](../api/circuit.html) represents a circuit of arbitrary topology, consisting of an arbitrary number of N-ports [Networks](../api/network.html) connected together. Like in an electronic circuit simulator, the circuit must have one (or more) `Port` connected to the circuit. The `Circuit` object allows one retrieving the M-ports `Network` (and thus its network parameters: $S$, $Z$, etc.), where M is the number of ports defined. Moreover, the `Circuit` object also allows calculating the scattering matrix $S$ of the entire circuit, that is the \"internal\" scattering matrices for the various intersections in the circuit. The calculation algorithm is based on ref [[1](#ref1)]. \n",
23 "\n",
24 "The figure below illustrates a network with 2 ports, `Network` elements $N_i$ and intersections:\n",
25 "\n",
26 "![General Circuit](figures/circuit_general.svg)\n",
27 "\n",
28 "\n",
29 "one must must define the connection list (\"netlist\") of the circuit. This connection list is defined as a List of List of interconnected Tuples `(network, port_number)`:\n",
30 "\n",
31 "```\n",
32 "connexions = [\n",
33 " [(network1, network1_port_nb), (network2, network2_port_nb), (network2, network2_port_nb), ...],\n",
34 " ...\n",
35 "]\n",
36 "```\n",
37 "\n",
38 "For example, the connection list to construct the above circuit could be: \n",
39 "\n",
40 "```\n",
41 "connexions = [\n",
42 " [(port1, 0), (network1, 0), (network4, 0)],\n",
43 " [(network1, 1), (network2, 0), (network5, 0)],\n",
44 " [(network1, 2), (network3, 0)],\n",
45 " [(network2, 1), (network3, 1)],\n",
46 " [(network2, 2), (port2, 0)],\n",
47 " [(network5, 1), (ground1, 0)],\n",
48 " [(network5, 2), (open1, 0)]\n",
49 "]\n",
50 "```\n",
51 "\n",
52 "where we have assumed that `port1`, `port2`, `ground1`, `open1` and all the `network1` to `network5` are scikit-rf [Networks](../api/network.html) objects with same `Frequency`. Networks can have different (real) characteristic impedances: mismatch are taken into account. Convenience methods are provided to create ports and grounded connexions:\n",
53 "\n",
54 "* [Circuit.Port()](../api/generated/skrf.circuit.Circuit.Port.html#skrf.circuit.Circuit.Port)\n",
55 "* [Circuit.Ground()](../api/generated/skrf.circuit.Circuit.Ground.html#skrf.circuit.Circuit.Ground)\n",
56 "* [Circuit.SeriesImpedance()](../api/generated/skrf.circuit.Circuit.SeriesImpedance.html#skrf.circuit.Circuit.SeriesImpedance)\n",
57 "* [Circuit.ShuntAdmittance()](../api/generated/skrf.circuit.Circuit.ShuntAdmittance.html#skrf.circuit.Circuit.ShuntAdmittance)\n",
58 "* [Circuit.Open()](../api/generated/skrf.circuit.Circuit.Open.html#skrf.circuit.Circuit.Open)"
59 ]
60 },
61 {
62 "cell_type": "markdown",
63 "metadata": {},
64 "source": [
65 "**Warnings**\n",
66 "\n",
67 "[Circuit](https://scikit-rf.readthedocs.io/en/latest/api/circuit.html) requires distinct `Network` names for each `Network`. So, in case you would like to use multiple times the same `Network`, you have to duplicate this `Network` (for example using the `.copy()` method) and to specify a distinct `.name` property for each copy, like:\n",
68 "```\n",
69 "# Assuming network1 is the Network you would like to duplicate:\n",
70 "network2 = network1.copy()\n",
71 "network1.name = 'My first Network'\n",
72 "network2.name = 'My second network'\n",
73 "```\n",
74 "\n",
75 "In addition, a set `(network, port_number)` should appear only once in the connection list. In case you would like to connect multiple networks to a single ones (like grounding multiple networks), there is two solutions:\n",
76 "* connecting the N networks to a single `Ground` object once like in:\n",
77 "```\n",
78 "# in the connection list:\n",
79 "...\n",
80 " [(network1,1),(network2,1),(network3,1),(gnd,0)]\n",
81 "...\n",
82 "```\n",
83 "* Or create as many distinct `Ground` objects (also with unique `name` properties as mentioned above) as necessary.\n",
84 "```\n",
85 "# in the connection list:\n",
86 "...\n",
87 " [(network1,1), (gnd1,0)],\n",
88 " [(network2,1), (gnd2,0)],\n",
89 " [(network3,1), (gnd3,0)],\n",
90 "...\n",
91 "```"
92 ]
93 },
94 {
95 "cell_type": "markdown",
96 "metadata": {},
97 "source": [
98 "Once the connection list is defined, the `Circuit` with:\n",
99 "\n",
100 "```\n",
101 "resulting_circuit = rf.Circuit(connexions)\n",
102 "```\n",
103 "`resulting_circuit` is a [Circuit](../api/circuit.html) object. \n",
104 "\n",
105 "The resulting 2-ports `Network` is obtained with the [Circuit.network](../api/generated/skrf.circuit.Circuit.network.html#skrf.circuit.Circuit.network) parameter:\n",
106 "\n",
107 "```\n",
108 "resulting_network = resulting_circuit.network\n",
109 "```"
110 ]
111 },
112 {
113 "cell_type": "markdown",
114 "metadata": {},
115 "source": [
116 "Note that it is also possible to create manually a circuit of multiple `Network` objects using the [connecting methods](../api/network.html#connecting-networks) of `scikit-rf`. Although the `Circuit` approach to build a multiple `Network` may appear to be more verbose than the 'classic' way for building a circuit, as the circuit complexity increases, in particular when components are connected in parallel, the `Circuit` approach is interesting as it increases the readability of the code. Moreover, `Circuit` circuit topology can be plotted using its `plot_graph` method, which is useful to rapidly control if the circuit is built as expected. "
117 ]
118 },
119 {
120 "cell_type": "markdown",
121 "metadata": {},
122 "source": [
123 "## Examples\n",
124 "### Loaded transmission line\n",
125 "Assume that a $50\\Omega$ lossless transmission line is loaded with a $Z_L=75\\Omega$ impedance. \n",
126 "\n",
127 "![Loaded Transmission Line Circuit](figures/circuit_loaded_transmission_line1.svg)\n",
128 "\n",
129 "If the transmission line electric length is $\\theta=0$, then one would thus expect the reflection coefficient to be:\n",
130 "\n",
131 "$$\n",
132 "\\rho = s = \\frac{Z_L - Z_0}{Z_L + Z_0} = 0.2\n",
133 "$$"
134 ]
135 },
136 {
137 "cell_type": "code",
138 "execution_count": null,
139 "metadata": {},
140 "outputs": [],
141 "source": [
142 "import skrf as rf\n",
143 "\n",
144 "rf.stylely()"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "execution_count": null,
150 "metadata": {},
151 "outputs": [],
152 "source": [
153 "Z_0 = 50\n",
154 "Z_L = 75\n",
155 "theta = 0\n",
156 "\n",
157 "# the necessary Frequency description\n",
158 "freq = rf.Frequency(start=1, stop=2, unit='GHz', npoints=3)\n",
159 "\n",
160 "# The combination of a transmission line + a load can be created\n",
161 "# using the convenience delay_load method\n",
162 "# important: all the Network must have the parameter \"name\" defined\n",
163 "tline_media = rf.DefinedGammaZ0(freq, z0=Z_0)\n",
164 "delay_load = tline_media.delay_load(rf.zl_2_Gamma0(Z_0, Z_L), theta, unit='deg', name='delay_load')\n",
165 "\n",
166 "# the input port of the circuit is defined with the Circuit.Port method\n",
167 "port1 = rf.Circuit.Port(freq, 'port1', z0=Z_0)\n",
168 "\n",
169 "# connection list\n",
170 "cnx = [\n",
171 " [(port1, 0), (delay_load, 0)]\n",
172 "]\n",
173 "# building the circuit\n",
174 "cir = rf.Circuit(cnx)\n",
175 "\n",
176 "# getting the resulting Network from the 'network' parameter:\n",
177 "ntw = cir.network\n",
178 "print(ntw)"
179 ]
180 },
181 {
182 "cell_type": "code",
183 "execution_count": null,
184 "metadata": {},
185 "outputs": [],
186 "source": [
187 "# as expected the reflection coefficient is:\n",
188 "print(ntw.s[0])"
189 ]
190 },
191 {
192 "cell_type": "markdown",
193 "metadata": {},
194 "source": [
195 "It is also possible to build the above circuit using a series impedance Network, then shorted:\n",
196 "\n",
197 "![Loaded Transmission Line Circuit: 2nd version](figures/circuit_loaded_transmission_line2.svg)\n",
198 "\n",
199 "To do so, one would need to use the `Ground()` method to generate the required `Network` object. "
200 ]
201 },
202 {
203 "cell_type": "code",
204 "execution_count": null,
205 "metadata": {},
206 "outputs": [],
207 "source": [
208 "port1 = rf.Circuit.Port(freq, 'port1', z0=Z_0)\n",
209 "# piece of transmission line and series impedance\n",
210 "trans_line = tline_media.line(theta, unit='deg', name='trans_line')\n",
211 "load = tline_media.resistor(Z_L, name='delay_load')\n",
212 "# ground network (short)\n",
213 "ground = rf.Circuit.Ground(freq, name='ground')\n",
214 "\n",
215 "# connection list\n",
216 "cnx = [\n",
217 " [(port1, 0), (trans_line, 0)],\n",
218 " [(trans_line, 1), (load, 0)],\n",
219 " [(load, 1), (ground, 0)]\n",
220 "]\n",
221 "# building the circuit\n",
222 "cir = rf.Circuit(cnx)\n",
223 "# the result if the same :\n",
224 "print(cir.network.s[0])\n"
225 ]
226 },
227 {
228 "cell_type": "markdown",
229 "metadata": {},
230 "source": [
231 "### LC Filter\n",
232 "Here we model a low-pass LC filter, with example values taken from [rf-tools.com](https://rf-tools.com/lc-filter/) :\n",
233 "\n",
234 "![low pass filter](figures/circuit_filter1.svg)"
235 ]
236 },
237 {
238 "cell_type": "code",
239 "execution_count": null,
240 "metadata": {},
241 "outputs": [],
242 "source": [
243 "freq = rf.Frequency(start=0.1, stop=10, unit='GHz', npoints=1001)\n",
244 "tl_media = rf.DefinedGammaZ0(freq, z0=50, gamma=1j*freq.w/rf.c)\n",
245 "C1 = tl_media.capacitor(3.222e-12, name='C1')\n",
246 "C2 = tl_media.capacitor(82.25e-15, name='C2')\n",
247 "C3 = tl_media.capacitor(3.222e-12, name='C3')\n",
248 "L2 = tl_media.inductor(8.893e-9, name='L2')\n",
249 "RL = tl_media.resistor(50, name='RL')\n",
250 "gnd = rf.Circuit.Ground(freq, name='gnd')\n",
251 "port1 = rf.Circuit.Port(freq, name='port1', z0=50)\n",
252 "port2 = rf.Circuit.Port(freq, name='port2', z0=50)\n",
253 "\n",
254 "cnx = [\n",
255 " [(port1, 0), (C1, 0), (L2, 0), (C2, 0)],\n",
256 " [(L2, 1), (C2, 1), (C3, 0), (port2, 0)],\n",
257 " [(gnd, 0), (C1, 1), (C3, 1)],\n",
258 "]\n",
259 "cir = rf.Circuit(cnx)\n",
260 "ntw = cir.network"
261 ]
262 },
263 {
264 "cell_type": "code",
265 "execution_count": null,
266 "metadata": {},
267 "outputs": [],
268 "source": [
269 "ntw.plot_s_db(m=0, n=0, lw=2, logx=True)\n",
270 "ntw.plot_s_db(m=1, n=0, lw=2, logx=True)"
271 ]
272 },
273 {
274 "cell_type": "markdown",
275 "metadata": {},
276 "source": [
277 "When building a `Circuit` made of few networks, it can be useful to represent the connection graphically, in order to check for possible errors. This is possible using the [Circuit.plot_graph()](../api/circuit.html#representing-a-circuit) method. Ports are indicated by triangles, Network with squares and interconnections with circles. It is possible to display the network names as well as their associated ports (and characteristic impedances): "
278 ]
279 },
280 {
281 "cell_type": "code",
282 "execution_count": null,
283 "metadata": {},
284 "outputs": [],
285 "source": [
286 "cir.plot_graph(network_labels=True, network_fontsize=15,\n",
287 " port_labels=True, port_fontsize=15,\n",
288 " edge_labels=True, edge_fontsize=10)"
289 ]
290 },
291 {
292 "cell_type": "markdown",
293 "metadata": {},
294 "source": [
295 "### Circuit Reduction\n",
296 "Here we model a slightly more complex band-pass LC filter to demonstrate circuit reduction, with example values taken from [markimicrowave.com](https://markimicrowave.com/technical-resources/tools/lc-filter-design-tool/) :\n",
297 "\n",
298 "![band pass filter](figures/circuit_filter2.svg)"
299 ]
300 },
301 {
302 "cell_type": "code",
303 "execution_count": null,
304 "metadata": {},
305 "outputs": [],
306 "source": [
307 "freq = rf.Frequency(50, 200, 301, 'mhz')\n",
308 "tl_media = rf.media.DefinedGammaZ0(frequency=freq, z0=50)\n",
309 "gnd = rf.Circuit.Ground(frequency=freq,name='ground')\n",
310 "C1 = tl_media.capacitor(6.353e-12, name='C1')\n",
311 "L1 = tl_media.inductor(402.7e-9, name='L1')\n",
312 "C2 = tl_media.capacitor(61.08e-12, name='C2')\n",
313 "L2 = tl_media.inductor(13.63e-9, name='L2')\n",
314 "C3 = tl_media.capacitor(187.8e-12, name='C3')\n",
315 "L3 = tl_media.inductor(41.89e-9, name='L3')\n",
316 "C4 = tl_media.capacitor(6.353e-12, name='C4')\n",
317 "L4 = tl_media.inductor(402.27e-9, name='L4')\n",
318 "Port1 = rf.Circuit.Port(frequency=freq, name='PortIn')\n",
319 "Port2 = rf.Circuit.Port(frequency=freq, name='PortOut')\n",
320 "cnx = [\n",
321 " [(Port1, 0), (C1, 0)],\n",
322 " [(C1, 1), (L1, 0)],\n",
323 " [(L1, 1), (C2, 0), (C3, 0), (C4, 0)],\n",
324 " [(C2, 1), (L2, 0)],\n",
325 " [(C3, 1), (L3, 0)],\n",
326 " [(L2, 1), (L3, 1), (gnd, 0)],\n",
327 " [(C4, 1), (L4, 0)],\n",
328 " [(L4, 1), (Port2, 0)]\n",
329 "]\n",
330 "cir = rf.Circuit(cnx)\n",
331 "ntw = cir.network"
332 ]
333 },
334 {
335 "cell_type": "code",
336 "execution_count": null,
337 "metadata": {},
338 "outputs": [],
339 "source": [
340 "ntw.plot_s_db(m=0, n=0, lw=2)\n",
341 "ntw.plot_s_db(m=1, n=0, lw=2)"
342 ]
343 },
344 {
345 "cell_type": "markdown",
346 "metadata": {},
347 "source": [
348 "As in the previous example, we can use [Circuit.plot_graph()](../api/circuit.html#representing-a-circuit) method to represent the connection graphically."
349 ]
350 },
351 {
352 "cell_type": "code",
353 "execution_count": null,
354 "metadata": {},
355 "outputs": [],
356 "source": [
357 "cir.plot_graph(network_labels=True, network_fontsize=15,\n",
358 " port_labels=True, port_fontsize=15,\n",
359 " edge_labels=True, edge_fontsize=10)"
360 ]
361 },
362 {
363 "cell_type": "markdown",
364 "metadata": {},
365 "source": [
366 "When focusing solely on `Ports` and disregarding internal `Networks`, we could utilize [rf.reduce_circuit()](../api/circuit.html#circuit-reduction) to streamline the circuit. This method will automatically series-connect adjacent networks through a `rf.connect()` approach, retaining only `Ports` and connections containing more than 2 `Networks` for circuit analysis."
367 ]
368 },
369 {
370 "cell_type": "code",
371 "execution_count": null,
372 "metadata": {},
373 "outputs": [],
374 "source": [
375 "reduced_cnx = rf.reduce_circuit(cnx)\n",
376 "reduced_cir = rf.Circuit(reduced_cnx)\n",
377 "reduced_cir.plot_graph(network_labels=True, network_fontsize=15,\n",
378 " port_labels=True, port_fontsize=15,\n",
379 " edge_labels=True, edge_fontsize=10)"
380 ]
381 },
382 {
383 "cell_type": "markdown",
384 "metadata": {},
385 "source": [
386 "Additionally, optional parameters can influence the behavior of circuit reduction. For instance, `split_ground=True` indicates splitting the shared `Ground` in the circuit:"
387 ]
388 },
389 {
390 "cell_type": "code",
391 "execution_count": null,
392 "metadata": {},
393 "outputs": [],
394 "source": [
395 "fully_reduced_cnx = rf.reduce_circuit(cnx, split_ground=True)\n",
396 "fully_reduced_cir = rf.Circuit(fully_reduced_cnx)\n",
397 "\n",
398 "fully_reduced_cir.plot_graph(network_labels=True, network_fontsize=15,\n",
399 " port_labels=True, port_fontsize=15,\n",
400 " edge_labels=True, edge_fontsize=10)"
401 ]
402 },
403 {
404 "cell_type": "markdown",
405 "metadata": {},
406 "source": [
407 "Finally, the Circuit constructor offers an option for automatic circuit simplification. Setting `auto_reduce=True` enables the creation of a significantly streamlined `Circuit` object, enhancing efficiency for Port's S-parameter calculations."
408 ]
409 },
410 {
411 "cell_type": "code",
412 "execution_count": null,
413 "metadata": {},
414 "outputs": [],
415 "source": [
416 "from timeit import timeit\n",
417 "\n",
418 "import numpy as np\n",
419 "\n",
420 "fully_reduced_ntw = fully_reduced_cir.network\n",
421 "auto_reduced_cir = rf.Circuit(cnx, auto_reduce=True)\n",
422 "auto_reduced_ntw = auto_reduced_cir.network\n",
423 "n_times = 10\n",
424 "print(\"Unreduced Circuit calculate Network in \"\n",
425 " f\"{timeit(lambda: cir.network, number=n_times)/n_times:.4f}s\")\n",
426 "print(\"Auto-reduced Circuit calculate Network in \"\n",
427 " f\"{timeit(lambda: auto_reduced_cir.network, number=n_times)/n_times:.4f}s\")\n",
428 "\n",
429 "print(\"Manually reduced circuit has the same s-parameters as the original circuit: \"\n",
430 " f\"{np.allclose(ntw.s, fully_reduced_ntw.s)}\")\n",
431 "print(\"Auto reduced circuit has the same s-parameters as the original circuit: \"\n",
432 " f\"{np.allclose(ntw.s, auto_reduced_ntw.s)}\")"
433 ]
434 },
435 {
436 "cell_type": "markdown",
437 "metadata": {},
438 "source": [
439 "## References\n",
440 "\n",
441 "<div id=\"ref1\"></div>[1] Hallbjörner, P., 2003. Method for calculating the scattering matrix of arbitrary microwave networks giving both internal and external scattering. Microw. Opt. Technol. Lett. 38, 99–102. https://doi.org/10/d27t7m\n"
442 ]
443 },
444 {
445 "cell_type": "code",
446 "execution_count": null,
447 "metadata": {},
448 "outputs": [],
449 "source": []
450 }
451 ],
452 "metadata": {
453 "celltoolbar": "Raw Cell Format",
454 "kernelspec": {
455 "display_name": "Python 3 (ipykernel)",
456 "language": "python",
457 "name": "python3"
458 },
459 "language_info": {
460 "codemirror_mode": {
461 "name": "ipython",
462 "version": 3
463 },
464 "file_extension": ".py",
465 "mimetype": "text/x-python",
466 "name": "python",
467 "nbconvert_exporter": "python",
468 "pygments_lexer": "ipython3",
469 "version": "3.8.10"
470 }
471 },
472 "nbformat": 4,
473 "nbformat_minor": 4
474 }