Debian Codegraph @

doc/source/examples/circuit/Lumped Element Circuits.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Lumped Elements Circuits"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "In this notebook, we construct various network from basic lumped elements (resistor, capacitor, inductor), with the 'classic' and the `Circuit` approach. Generally the `Circuit` approach is more verbose than the 'classic' way for building a circuit. However, 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` object can be plotted using its `plot_graph()` method, which is useful to rapidly control if the circuit is built as expected. "
15 ]
16 },
17 {
18 "cell_type": "code",
19 "execution_count": null,
20 "metadata": {},
21 "outputs": [],
22 "source": [
23 "import numpy as np # for np.allclose() to check that S-params are similar\n",
24 "\n",
25 "import skrf as rf\n",
26 "\n",
27 "rf.stylely()"
28 ]
29 },
30 {
31 "cell_type": "markdown",
32 "metadata": {},
33 "source": [
34 "## LC Series Circuit"
35 ]
36 },
37 {
38 "cell_type": "markdown",
39 "metadata": {},
40 "source": [
41 "In this section we reproduce a simple equivalent model of a capacitor $C$, as illustrated by the figure below:\n",
42 "\n",
43 "<img src=\"designer_capacitor_simple.png\" width=\"700\">\n"
44 ]
45 },
46 {
47 "cell_type": "code",
48 "execution_count": null,
49 "metadata": {},
50 "outputs": [],
51 "source": [
52 "# reference LC circuit made in Designer\n",
53 "LC_designer = rf.Network('designer_capacitor_30_80MHz_simple.s2p')"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": null,
59 "metadata": {},
60 "outputs": [],
61 "source": [
62 "# scikit-rf: manually connecting networks\n",
63 "line = rf.media.DefinedGammaZ0(frequency=LC_designer.frequency, z0=50)\n",
64 "LC_manual = line.inductor(24e-9) ** line.capacitor(70e-12)"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": null,
70 "metadata": {},
71 "outputs": [],
72 "source": [
73 "# scikit-rf: using Circuit builder\n",
74 "port1 = rf.Circuit.Port(frequency=LC_designer.frequency, name='port1', z0=50)\n",
75 "port2 = rf.Circuit.Port(frequency=LC_designer.frequency, name='port2', z0=50)\n",
76 "cap = rf.Circuit.SeriesImpedance(frequency=LC_designer.frequency, name='cap', z0=50,\n",
77 " Z=1/(1j*LC_designer.frequency.w*70e-12))\n",
78 "ind = rf.Circuit.SeriesImpedance(frequency=LC_designer.frequency, name='ind', z0=50,\n",
79 " Z=1j*LC_designer.frequency.w*24e-9)\n",
80 "\n",
81 "# NB: it is also possible to create 2-port lumped elements like:\n",
82 "# line = rf.media.DefinedGammaZ0(frequency=LC_designer.frequency, z0=50)\n",
83 "# cap = line.capacitor(70e-12, name='cap')\n",
84 "# ind = line.inductor(24e-9, name='ind')\n",
85 "\n",
86 "connections = [\n",
87 " [(port1, 0), (cap, 0)],\n",
88 " [(cap, 1), (ind, 0)],\n",
89 " [(ind, 1), (port2, 0)]\n",
90 "]\n",
91 "circuit = rf.Circuit(connections)\n",
92 "LC_from_circuit = circuit.network"
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": null,
98 "metadata": {},
99 "outputs": [],
100 "source": [
101 "# testing the equivalence of the results\n",
102 "print(np.allclose(LC_designer.s, LC_manual.s))\n",
103 "print(np.allclose(LC_designer.s, LC_from_circuit.s))"
104 ]
105 },
106 {
107 "cell_type": "code",
108 "execution_count": null,
109 "metadata": {},
110 "outputs": [],
111 "source": [
112 "circuit.plot_graph(network_labels=True, edge_labels=True, port_labels=True)"
113 ]
114 },
115 {
116 "cell_type": "markdown",
117 "metadata": {},
118 "source": [
119 "## A More Advanced Equivalent Model"
120 ]
121 },
122 {
123 "cell_type": "markdown",
124 "metadata": {},
125 "source": [
126 "In this section we reproduce an equivalent model of a capacitor $C$, as illustrated by the figure below:\n",
127 "\n",
128 "<img src=\"designer_capacitor_adv.png\" width=\"800\">"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": null,
134 "metadata": {},
135 "outputs": [],
136 "source": [
137 "# Reference results from ANSYS Designer\n",
138 "LCC_designer = rf.Network('designer_capacitor_30_80MHz_adv.s2p')"
139 ]
140 },
141 {
142 "cell_type": "code",
143 "execution_count": null,
144 "metadata": {},
145 "outputs": [],
146 "source": [
147 "# scikit-rf: usual way, but this time this is more tedious to deal with connection and port number\n",
148 "freq = LCC_designer.frequency\n",
149 "line = rf.media.DefinedGammaZ0(frequency=freq, z0=50)\n",
150 "elements1 = line.resistor(1e-2) ** line.inductor(24e-9) ** line.capacitor(70e-12)\n",
151 "elements2 = line.resistor(20e6)\n",
152 "T_in = line.tee()\n",
153 "T_out = line.tee()\n",
154 "ntw = rf.connect(T_in, 1, elements1, 0)\n",
155 "ntw = rf.connect(ntw, 2, elements2, 0)\n",
156 "ntw = rf.connect(ntw, 1, T_out, 1)\n",
157 "ntw = rf.innerconnect(ntw, 1, 2)\n",
158 "LCC_manual = ntw ** line.shunt_capacitor(50e-12)"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": null,
164 "metadata": {},
165 "outputs": [],
166 "source": [
167 "# scikit-rf: using Circuit builder\n",
168 "freq = LCC_designer.frequency\n",
169 "port1 = rf.Circuit.Port(frequency=freq, name='port1', z0=50)\n",
170 "port2 = rf.Circuit.Port(frequency=freq, name='port2', z0=50)\n",
171 "line = rf.media.DefinedGammaZ0(frequency=freq, z0=50)\n",
172 "cap = line.capacitor(70e-12, name='cap')\n",
173 "ind = line.inductor(24e-9, name='ind')\n",
174 "res_series = line.resistor(1e-2, name='res_series')\n",
175 "res_parallel = line.resistor(20e6, name='res_parallel')\n",
176 "cap_shunt = line.capacitor(50e-12, name='cap_shunt')\n",
177 "ground = rf.Circuit.Ground(frequency=freq, name='ground', z0=50)\n",
178 "\n",
179 "connections = [\n",
180 " [(port1, 0), (res_series, 0), (res_parallel, 0)],\n",
181 " [(res_series, 1), (cap, 0)],\n",
182 " [(cap, 1), (ind, 0)],\n",
183 " [(ind, 1), (cap_shunt, 0), (res_parallel, 1), (port2, 0)],\n",
184 " [(cap_shunt, 1), (ground, 0)],\n",
185 "]\n",
186 "circuit = rf.Circuit(connections)\n",
187 "LCC_from_circuit = circuit.network"
188 ]
189 },
190 {
191 "cell_type": "code",
192 "execution_count": null,
193 "metadata": {},
194 "outputs": [],
195 "source": [
196 "# testing the equivalence of the results\n",
197 "print(np.allclose(LCC_designer.s, LCC_manual.s))\n",
198 "print(np.allclose(LCC_designer.s, LCC_from_circuit.s))"
199 ]
200 },
201 {
202 "cell_type": "code",
203 "execution_count": null,
204 "metadata": {},
205 "outputs": [],
206 "source": [
207 "circuit.plot_graph(network_labels=True, edge_labels=True, port_labels=True)"
208 ]
209 },
210 {
211 "cell_type": "markdown",
212 "metadata": {},
213 "source": [
214 "## Pass band filter"
215 ]
216 },
217 {
218 "cell_type": "markdown",
219 "metadata": {},
220 "source": [
221 "Below we construct a pass-band filter, from an example given in [Microwaves101](https://www.microwaves101.com/encyclopedias/lumped-element-filter-calculator):"
222 ]
223 },
224 {
225 "cell_type": "markdown",
226 "metadata": {},
227 "source": [
228 "<img src=\"designer_bandpass_filter_450_550MHz.png\" width=\"800\">\n"
229 ]
230 },
231 {
232 "cell_type": "code",
233 "execution_count": null,
234 "metadata": {},
235 "outputs": [],
236 "source": [
237 "# Reference result calculated from Designer\n",
238 "passband_designer = rf.Network('designer_bandpass_filter_450_550MHz.s2p')"
239 ]
240 },
241 {
242 "cell_type": "code",
243 "execution_count": null,
244 "metadata": {},
245 "outputs": [],
246 "source": [
247 "# scikit-rf: the filter by cascading all lumped-elements\n",
248 "freq = passband_designer.frequency\n",
249 "passband_manual = line.shunt_capacitor(25.406e-12) ** line.shunt_inductor(4.154e-9) ** \\\n",
250 " line.capacitor(2.419e-12) ** line.inductor(43.636e-9) ** \\\n",
251 " line.shunt_capacitor(25.406e-12) ** line.shunt_inductor(4.154e-9)"
252 ]
253 },
254 {
255 "cell_type": "code",
256 "execution_count": null,
257 "metadata": {},
258 "outputs": [],
259 "source": [
260 "# scikit-rf: the filter with the Circuit builder\n",
261 "freq = passband_designer.frequency\n",
262 "line = rf.media.DefinedGammaZ0(frequency=freq)\n",
263 "C1 = line.capacitor(25.406e-12, name='C1')\n",
264 "C2 = line.capacitor(2.419e-12, name='C2')\n",
265 "C3 = line.capacitor(25.406e-12, name='C3')\n",
266 "L1 = line.inductor(4.154e-9, name='L1')\n",
267 "L2 = line.inductor(43.636e-9, name='L2')\n",
268 "L3 = line.inductor(4.154e-9, name='L3')\n",
269 "port1 = rf.Circuit.Port(frequency=freq, name='port1', z0=50)\n",
270 "port2 = rf.Circuit.Port(frequency=freq, name='port2', z0=50)\n",
271 "ground = rf.Circuit.Ground(frequency=freq, name='ground', z0=50)\n",
272 "\n",
273 "connections = [\n",
274 " [(port1, 0), (C1, 0), (L1, 0), (C2, 0)],\n",
275 " [(C2, 1), (L2, 0)],\n",
276 " [(L2, 1), (C3, 0), (L3, 0), (port2, 0)],\n",
277 " [(C1, 1), (C3, 1), (L1, 1), (L3, 1), (ground, 0)],\n",
278 "]\n",
279 "\n",
280 "circuit = rf.Circuit(connections)\n",
281 "passband_circuit = circuit.network\n",
282 "passband_circuit.name = 'Pass-band circuit'"
283 ]
284 },
285 {
286 "cell_type": "code",
287 "execution_count": null,
288 "metadata": {},
289 "outputs": [],
290 "source": [
291 "passband_circuit.plot_s_db(m=0, n=0, lw=2)\n",
292 "passband_circuit.plot_s_db(m=1, n=0, lw=2)\n",
293 "passband_designer.plot_s_db(m=0, n=0, lw=2, ls='-.')\n",
294 "passband_designer.plot_s_db(m=1, n=0, lw=2, ls='-.')"
295 ]
296 },
297 {
298 "cell_type": "code",
299 "execution_count": null,
300 "metadata": {},
301 "outputs": [],
302 "source": [
303 "circuit.plot_graph(network_labels=True, port_labels=True, edge_labels=True)"
304 ]
305 },
306 {
307 "cell_type": "code",
308 "execution_count": null,
309 "metadata": {},
310 "outputs": [],
311 "source": []
312 }
313 ],
314 "metadata": {
315 "kernelspec": {
316 "display_name": "Python 3.10.6 ('.venv': venv)",
317 "language": "python",
318 "name": "python3"
319 },
320 "language_info": {
321 "codemirror_mode": {
322 "name": "ipython",
323 "version": 3
324 },
325 "file_extension": ".py",
326 "mimetype": "text/x-python",
327 "name": "python",
328 "nbconvert_exporter": "python",
329 "pygments_lexer": "ipython3",
330 "version": "3.10.6"
331 },
332 "vscode": {
333 "interpreter": {
334 "hash": "637c323cf467337602e9974a89cb4d3fc95fac3ef875a73e62754f8e768d8de7"
335 }
336 }
337 },
338 "nbformat": 4,
339 "nbformat_minor": 4
340 }