Debian Codegraph @

skrf/media/distributedCircuit.py

raw symbols SCIP
1 """
2 distributedCircuit (:mod:`skrf.media.distributedCircuit`)
3 ============================================================
4
5 A transmission line mode defined in terms of distributed impedance and admittance values.
6
7 .. autosummary::
8 :toctree: generated/
9
10 DistributedCircuit
11
12 """
13 from __future__ import annotations
14
15 from collections.abc import Iterable
16 from typing import TYPE_CHECKING
17
18 from numpy import array, asarray, imag, real, sqrt
19
20 from ..constants import NumberLike
21 from .media import DefinedGammaZ0, Media
22
23 if TYPE_CHECKING:
24 from ..frequency import Frequency
25
26
27 class DistributedCircuit(Media):
28 r"""
29 A transmission line mode defined in terms of distributed impedance and admittance values.
30
31 Parameters
32 ------------
33 frequency : :class:`~skrf.frequency.Frequency` object
34 frequency band of the media
35 z0_port : number, array-like, or None
36 `z0_port` is the port impedance for networks generated by the media.
37 If `z0_port` is not None, the networks generated by the media are
38 renormalized (or in other words embedded) from the characteristic
39 impedance z0 of the media to `z0_port`.
40 Else if `z0_port` is None, the networks port impedances will be the raw
41 characteristic impedance z0 of the media.
42 (Default is None)
43 z0_override : number, array-like, or None
44 `z0_override` override the characteristic impedance for the media.
45 If `z0_override` is not None, the networks generated by the media have
46 their characteristic impedance `z0` overridden by `z0_override`.
47 (Default is None)
48 z0 : number, array-like, or None
49 deprecated parameter, alias to `z0_override` if `z0_override` is None.
50 Emit a deprecation warning.
51 C : number, or array-like
52 distributed capacitance, in F/m
53 L : number, or array-like
54 distributed inductance, in H/m
55 R : number, or array-like
56 distributed resistance, in Ohm/m
57 G : number, or array-like
58 distributed conductance, in S/m
59
60
61 Notes
62 -----
63 if C,L,R,G are vectors they should be the same length
64
65 :class:`DistributedCircuit` is `Media` object representing a
66 transmission line mode defined in terms of distributed impedance
67 and admittance values.
68
69 A `DistributedCircuit` may be defined in terms
70 of the following attributes:
71
72 ================================ ================ ================
73 Quantity Symbol Property
74 ================================ ================ ================
75 Distributed Capacitance :math:`C^{'}` :attr:`C`
76 Distributed Inductance :math:`L^{'}` :attr:`L`
77 Distributed Resistance :math:`R^{'}` :attr:`R`
78 Distributed Conductance :math:`G^{'}` :attr:`G`
79 ================================ ================ ================
80
81
82 The following quantities may be calculated, which are functions of
83 angular frequency (:math:`\omega`):
84
85 =================================== ================================================== ========================
86 Quantity Symbol Property
87 =================================== ================================================== ========================
88 Distributed Impedance :math:`Z^{'} = R^{'} + j \omega L^{'}` :attr:`Z`
89 Distributed Admittance :math:`Y^{'} = G^{'} + j \omega C^{'}` :attr:`Y`
90 =================================== ================================================== ========================
91
92
93 The properties which define their wave behavior:
94
95 =================================== ============================================ ==============================
96 Quantity Symbol Method
97 =================================== ============================================ ==============================
98 Characteristic Impedance :math:`Z_0 = \sqrt{ \frac{Z^{'}}{Y^{'}}}` :func:`Z0`
99 Propagation Constant :math:`\gamma = \sqrt{ Z^{'} Y^{'}}` :func:`gamma`
100 =================================== ============================================ ==============================
101
102 Given the following definitions, the components of propagation
103 constant are interpreted as follows:
104
105 .. math::
106
107 +\Re e\{\gamma\} = \text{attenuation}
108
109 -\Im m\{\gamma\} = \text{forward propagation}
110
111
112 See Also
113 --------
114 from_media
115
116 """
117
118 def __init__(self, frequency: Frequency | None = None,
119 z0_port: NumberLike | None = None,
120 z0_override: NumberLike | None = None,
121 z0: NumberLike | None = None,
122 C: NumberLike = 90e-12, L: NumberLike = 280e-9,
123 R: NumberLike = 0, G: NumberLike = 0,
124 *args, **kwargs):
125 Media.__init__(self, frequency = frequency,
126 z0_port = z0_port, z0_override = z0_override, z0 = z0)
127
128 def _check_equal_length(*params):
129 "Check that all params have same length"
130 lengths = [asarray(p).size for p in params]
131
132 # allows either scalars or freq-size array
133 if all(l == 1 or l == self.frequency.npoints for l in lengths):
134 return True
135 else: # avoid mixed case
136 return False
137
138 if _check_equal_length(C, L, R, G):
139 self.C, self.L, self.R, self.G = C, L, R, G
140 else:
141 raise ValueError("Parameters C,L,R,G must have same length or be scalar.")
142
143 def _format_param(self, param):
144 if isinstance(param, Iterable) and not isinstance(param, (str, bytes)):
145 param = array(param)
146 return f"{param[0]:.2e}, ..., {param[-1]:.2e}"
147 else:
148 return f"{param:.2e}"
149
150 def __str__(self) -> str:
151 f = self.frequency
152 output = (
153 f'Distributed Circuit Media. {f.f_scaled[0]}-{f.f_scaled[-1]} {f.unit}. {f.npoints} points'
154 f'\nL\'= {self._format_param(self.L)}, C\'= {self._format_param(self.C)},'
155 f'R\'= {self._format_param(self.R)}, G\'= {self._format_param(self.G)}, ')
156 return output
157
158 def __repr__(self) -> str:
159 return self.__str__()
160
161 @classmethod
162 def from_media(cls, my_media: Media, *args, **kwargs) -> Media:
163 """
164 Initializes a `DistributedCircuit` from an existing
165 :class:`~skrf.media.media.Media` instance.
166
167 Parameters
168 ----------
169 my_media : :class:`~skrf.media.media.Media` instance.
170 the media object
171
172 See Also
173 --------
174 :class:`~skrf.media.media.Media`
175 """
176
177 w = my_media.frequency.w
178 gamma = my_media.gamma
179 z0 = my_media.z0
180 z0_port = my_media.z0_port
181
182 Y = gamma/z0
183 Z = gamma*z0
184 G,C = real(Y), imag(Y)/w
185 R,L = real(Z), imag(Z)/w
186 return cls(frequency = my_media.frequency,
187 z0_port = z0_port, C=C, L=L, R=R, G=G, **kwargs)
188
189 @classmethod
190 def from_csv(self, *args, **kw):
191 """
192 Create a `DistributedCircuit` from numerical values stored in a csv file.
193
194 The csv file format must be written by the function :func:`write_csv`,
195 or similar method which produces the following format::
196
197 f[$unit], Re(z0), Im(z0), Re(gamma), Im(gamma), Re(z0_port), Im(z0_port)
198 1, 1, 1, 1, 1, 1, 1
199 2, 1, 1, 1, 1, 1, 1
200 .....
201
202 See Also
203 --------
204 write_csv
205 """
206 d = DefinedGammaZ0.from_csv(*args,**kw)
207 return self.from_media(d)
208
209 @property
210 def Z(self) -> NumberLike:
211 r"""
212 Distributed Impedance, :math:`Z^{'}`.
213
214 Defined as
215
216 .. math::
217
218 Z^{'} = R^{'} + j \omega L^{'}
219
220 Returns
221 -------
222 Z : np.ndarray
223 Distributed impedance in units of ohm/m
224 """
225 w = self.frequency.w
226 Z = self.R + 1j*w*self.L
227 # Avoid divide by zero.
228 # Needs to be imaginary to avoid all divide by zeros in the media class.
229 Z[Z.imag == 0] += 1j*1e-12
230 return Z
231
232 @property
233 def Y(self) -> NumberLike:
234 r"""
235 Distributed Admittance, :math:`Y^{'}`.
236
237 Defined as
238
239 .. math::
240
241 Y^{'} = G^{'} + j \omega C^{'}
242
243 Returns
244 -------
245 Y : np.ndarray
246 Distributed Admittance in units of S/m
247 """
248
249 w = self.frequency.w
250 Y = self.G + 1j*w*self.C
251 # Avoid divide by zero.
252 # Needs to be imaginary to avoid all divide by zeros in the media class.
253 Y[Y.imag == 0] += 1j*1e-12
254 return Y
255
256 @property
257 def z0_characteristic(self) -> NumberLike:
258 r"""
259 Characteristic Impedance, :math:`z_0`
260
261 .. math::
262
263 Z_0 = \sqrt{ \frac{Z^{'}}{Y^{'}}}
264
265 Returns
266 -------
267 z0_characteristic : np.ndarray
268 Characteristic Impedance in units of ohms
269 """
270 return sqrt(self.Z/self.Y)
271
272 @property
273 def gamma(self) -> NumberLike:
274 r"""
275 Propagation Constant, :math:`\gamma`.
276
277 Defined as,
278
279 .. math::
280
281 \gamma = \sqrt{ Z^{'} Y^{'}}
282
283 Returns
284 -------
285 gamma : np.ndarray
286 Propagation Constant,
287
288 Note
289 ----
290 The components of propagation constant are interpreted as follows:
291
292 * positive real(gamma) = attenuation
293 * positive imag(gamma) = forward propagation
294 """
295 return sqrt(self.Z*self.Y)