Debian Codegraph @

skrf/media/tests/test_media.py

raw symbols SCIP
1 import os
2 import unittest
3
4 import numpy as np
5 from numpy.testing import assert_array_almost_equal
6
7 from skrf.constants import S_DEF_DEFAULT
8 from skrf.frequency import Frequency
9 from skrf.media import DefinedGammaZ0
10 from skrf.network import Network, concat_ports, connect
11
12
13 class DefinedGammaZ0TestCase(unittest.TestCase):
14 def setUp(self):
15 self.files_dir = os.path.join(
16 os.path.dirname(os.path.abspath(__file__)),
17 'qucs_prj'
18 )
19 self.dummy_media = DefinedGammaZ0(
20 frequency = Frequency(1, 100, 21, unit='ghz'),
21 gamma=1j,
22 z0 = 50 ,
23 )
24
25 def test_impedance_mismatch(self):
26 """
27 Test the naming of the network. When circuit is used to connect a
28 topology of networks, they should have unique names.
29 """
30 name = 'impedanceMismatch,50to25'
31 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
32 # sNp-files store the same impedance for every port, so to compare the
33 # networks, the port impedance has to be set manually to [50,25]
34 qucs_ntwk.z0 = [50,25]
35 self.dummy_media.frequency = qucs_ntwk.frequency
36 skrf_ntwk = self.dummy_media.thru(z0=50, name = name)**\
37 self.dummy_media.thru(z0=25)
38
39 self.assertEqual(qucs_ntwk, skrf_ntwk)
40 self.assertEqual(qucs_ntwk.name, skrf_ntwk.name)
41
42 def test_tee(self):
43 """
44 Test the naming of the network. When circuit is used to connect a
45 topology of networks, they should have unique names.
46 """
47 name = 'tee'
48 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
49 skrf_ntwk = self.dummy_media.tee(name = name)
50 self.assertEqual(name, skrf_ntwk.name)
51
52 def test_splitter(self):
53 """
54 Test the naming of the network. When circuit is used to connect a
55 topology of networks, they should have unique names.
56 Test mismatched splitter.
57 """
58 name = 'splitter'
59 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
60 skrf_ntwk = self.dummy_media.splitter(3, name = name)
61 self.assertEqual(name, skrf_ntwk.name)
62
63 # Check the s-parameters
64 s = np.zeros_like(skrf_ntwk.s, dtype='complex')
65 y0s = np.array(1./skrf_ntwk.z0)
66 y_k = y0s.sum(axis=1)
67 s = 2 *np.sqrt(np.einsum('ki,kj->kij', y0s, y0s)) / y_k[:, None, None]
68 np.einsum('kii->ki', s)[:] -= 1 # Sii
69 assert_array_almost_equal(skrf_ntwk.s, s)
70
71 def test_mismatch_splitter(self):
72 """
73 Test mismatched splitter against 50-ohm splitter connected to
74 equivalent mismatched thrus.
75 """
76 freq = Frequency(1, 1, 1, unit='GHz')
77 med = DefinedGammaZ0(frequency=freq, z0=50)
78
79 for n in range(2, 5):
80 z_port = np.arange(1, n + 1) * 20
81 split =med.splitter(n, z0=z_port)
82 thrus = []
83 for k in range(n):
84 thrus.append(med.thru(z0 = z_port[k]))
85
86 # connect
87 thru = concat_ports(thrus, port_order='second')
88 ref = med.splitter(n)
89 ref = connect(ref, 0, thru, 0, n)
90 assert_array_almost_equal(split.s, ref.s)
91
92 def test_complex_impedance_mismatch_tee(self):
93 """
94 Test the complex impedance mismatch.
95 """
96 z0 = (25+25j, 50, 75-25j)
97 ref = self.dummy_media.tee()
98 ref.renormalize(z_new=z0)
99 tee = self.dummy_media.tee(z0=z0)
100 assert_array_almost_equal(ref.s, tee.s)
101 assert_array_almost_equal(np.linalg.inv(tee.s), tee.s.conj())
102
103 def test_splitter_is_reciprocal_and_unitary(self):
104 """
105 Test the splitter's s-parameters is reciprocal and unitary matrix for
106 different port impedances.
107 """
108 # A unitary matrix satisfies the property that its inverse is equal to its conjugate transpose.
109 # Additionally, a reciprocal matrix satisfies the property that its transpose is equal to itself.
110 # Combining these properties, the inverse of the splitter's S-parameters is equal to its conjugate.
111 for z0 in ((50, 50, 50), (25, 50, 75), (25+25j, 50, 75-25j)):
112 # Test the reciprocal and unitary property for matched, mismatched and complex impedance Tee/splitters.
113 tee = self.dummy_media.tee(z0=z0)
114 assert_array_almost_equal(np.linalg.inv(tee.s), tee.s.conj())
115
116 def test_thru(self):
117 """
118 Test the naming of the network. When circuit is used to connect a
119 topology of networks, they should have unique names.
120 """
121 name = 'thru'
122 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
123 skrf_ntwk = self.dummy_media.thru(name = name)
124 self.assertEqual(name, skrf_ntwk.name)
125
126 def test_line(self):
127 """
128 Test the naming of the network. When circuit is used to connect a
129 topology of networks, they should have unique names.
130 """
131 name = 'line'
132 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
133 skrf_ntwk = self.dummy_media.line(90, 'deg', name = name)
134 self.assertEqual(name, skrf_ntwk.name)
135
136 def test_delay_load(self):
137 """
138 Test the naming of the network. When circuit is used to connect a
139 topology of networks, they should have unique names.
140 """
141 name = 'delay_load'
142 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
143 skrf_ntwk = self.dummy_media.delay_load(1j, 90, 'deg',
144 name = name)
145 self.assertEqual(name, skrf_ntwk.name)
146
147 def test_shunt_delay_load(self):
148 """
149 Test the naming of the network. When circuit is used to connect a
150 topology of networks, they should have unique names.
151 """
152 name = 'shunt_delay_load'
153 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
154 skrf_ntwk = self.dummy_media.shunt_delay_load(1j, 90, 'deg',
155 name = name)
156 self.assertEqual(name, skrf_ntwk.name)
157
158 def test_delay_open(self):
159 """
160 Test the naming of the network. When circuit is used to connect a
161 topology of networks, they should have unique names.
162 """
163 name = 'delay_open'
164 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
165 skrf_ntwk = self.dummy_media.delay_open(90, 'deg', name = name)
166 self.assertEqual(name, skrf_ntwk.name)
167
168 def test_shunt_delay_open(self):
169 """
170 Test the naming of the network. When circuit is used to connect a
171 topology of networks, they should have unique names.
172 """
173 name = 'shunt_delay_open'
174 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
175 skrf_ntwk = self.dummy_media.shunt_delay_open(90, 'deg', name = name)
176 self.assertEqual(name, skrf_ntwk.name)
177
178 def test_delay_short(self):
179 """
180 Test the naming of the network. When circuit is used to connect a
181 topology of networks, they should have unique names.
182 """
183 name = 'delay_short'
184 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
185 skrf_ntwk = self.dummy_media.delay_short(90, 'deg', name = name)
186 self.assertEqual(name, skrf_ntwk.name)
187
188 def test_shunt_delay_short(self):
189 """
190 Test the naming of the network. When circuit is used to connect a
191 topology of networks, they should have unique names.
192 """
193 name = 'shunt_delay_short'
194 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
195 skrf_ntwk = self.dummy_media.shunt_delay_short(90, 'deg', name = name)
196 self.assertEqual(name, skrf_ntwk.name)
197
198 def test_resistor(self):
199 """
200 Compare the component values against s-parameters generated by QUCS.
201 Test the naming of the network. When circuit is used to connect a
202 topology of networks, they should have unique names.
203 """
204 name = 'resistor,1ohm'
205 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
206 self.dummy_media.frequency = qucs_ntwk.frequency
207 skrf_ntwk = self.dummy_media.resistor(1, name = name)
208 self.assertEqual(qucs_ntwk, skrf_ntwk)
209 self.assertEqual(qucs_ntwk.name, skrf_ntwk.name)
210 # test vs analytical ABCD parameters of a series resistor
211 Z = 10 - 4j
212 ntwk = self.dummy_media.resistor(Z)
213 ABCD = np.full(ntwk.s.shape, [[1, -1+1j],
214 [0, 1]])
215 ABCD[:,0,1] = Z
216 assert_array_almost_equal(ABCD, ntwk.a)
217
218 def test_resistor_mismatch(self):
219 """
220 Compare the component values against s-parameters generated by QUCS.
221 The mismatch QUCS component is generated by renormalize() method.
222 """
223 # Config the qucs_ntwk and media
224 name = 'resistor,1ohm'
225 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
226 self.dummy_media.frequency = qucs_ntwk.frequency
227 mismatch_z0_tuple = ([25, 50], [25-5j, 50+10j], [25, 50-10j])
228
229 # Test against Y-parameter definition
230 def resistor_y_def(media: DefinedGammaZ0, R, **kwargs):
231 result = media.match(nports=2, **kwargs)
232 y = np.zeros(shape=result.s.shape, dtype=complex)
233 R = np.array(R)
234 y[:, 0, 0] = 1.0 / R
235 y[:, 1, 1] = 1.0 / R
236 y[:, 0, 1] = -1.0 / R
237 y[:, 1, 0] = -1.0 / R
238 result.y = y
239 return result
240
241 # Test real and imag port impedance with Y-parameter definition and renormalize()
242 for mismatch_z0 in mismatch_z0_tuple:
243 qucs_ntwk_copy = qucs_ntwk.copy()
244 qucs_ntwk_copy.renormalize(mismatch_z0)
245 skrf_ntwk = self.dummy_media.resistor(1, name = name, z0=mismatch_z0)
246 skrf_ntwk_y = resistor_y_def(self.dummy_media, 1, z0=mismatch_z0)
247 self.assertEqual(qucs_ntwk_copy, skrf_ntwk)
248 assert_array_almost_equal(skrf_ntwk.s, skrf_ntwk_y.s)
249
250 # Config the s_def parameters
251 mismatch_z0 = [25, 50-10j]
252 s_defs = ("power", "pseudo", "traveling")
253
254 # Test different s_def configurations
255 for s_def in s_defs:
256 qucs_ntwk_copy = qucs_ntwk.copy()
257 qucs_ntwk_copy.renormalize(mismatch_z0, s_def=s_def)
258 skrf_ntwk = self.dummy_media.resistor(1, name=name, z0=mismatch_z0, s_def=s_def)
259 skrf_ntwk_y = resistor_y_def(self.dummy_media, 1, z0=mismatch_z0, s_def=s_def)
260 self.assertEqual(skrf_ntwk, qucs_ntwk_copy)
261 self.assertEqual(skrf_ntwk.s_def, qucs_ntwk_copy.s_def)
262 assert_array_almost_equal(skrf_ntwk.s, skrf_ntwk_y.s)
263 self.assertEqual(skrf_ntwk.s_def, skrf_ntwk_y.s_def)
264
265 def test_shunt_resistor(self):
266 """
267 Test the naming of the network. When circuit is used to connect a
268 topology of networks, they should have unique names.
269 """
270 name = 'shunt_resistor,1ohm'
271 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
272 skrf_ntwk = self.dummy_media.resistor(1, name = name)
273 self.assertEqual(name, skrf_ntwk.name)
274 # test vs analytical ABCD parameters of a shunt resistor
275 Z = 10 - 4j
276 ntwk = self.dummy_media.shunt_resistor(Z)
277 ABCD = np.asarray([[[1, 0], [1/Z, 1]]])
278 assert_array_almost_equal(ABCD, ntwk.a)
279
280 def test_capacitor(self):
281 """
282 Compare the component values against s-parameters generated by QUCS.
283 Test the naming of the network. When circuit is used to connect a
284 topology of networks, they should have unique names.
285 """
286 name = 'capacitor,p01pF'
287 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
288 self.dummy_media.frequency = qucs_ntwk.frequency
289 skrf_ntwk = self.dummy_media.capacitor(.01e-12, name = name)
290 self.assertEqual(qucs_ntwk, skrf_ntwk)
291 self.assertEqual(qucs_ntwk.name, skrf_ntwk.name)
292 # test vs analytical ABCD parameters of a series capacitor
293 C = 0.1e-12
294 ntwk = self.dummy_media.capacitor(C)
295 Z = 1/(1j*C*ntwk.frequency.w)
296 ABCD = np.full(ntwk.s.shape, [[1, -1+1j],
297 [0, 1]])
298 ABCD[:,0,1] = Z
299 assert_array_almost_equal(ABCD, ntwk.a)
300
301 def test_capacitor_mismatch(self):
302 """
303 Compare the component values against s-parameters generated by QUCS.
304 The mismatch QUCS component is generated by renormalize() method.
305 """
306 # Config the qucs_ntwk and media
307 name = 'capacitor,p01pF'
308 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
309 self.dummy_media.frequency = qucs_ntwk.frequency
310 mismatch_z0_tuple = ([25, 50], [25-5j, 50+10j], [25, 50-10j])
311
312 # Test against Y-parameter definition
313 def capacitor_y_def(media: DefinedGammaZ0, C, **kwargs):
314 result = media.match(nports=2, **kwargs)
315 w = media.frequency.w
316 y = np.zeros(shape=result.s.shape, dtype=complex)
317 C = np.array(C)
318 y[:, 0, 0] = 1j * w * C
319 y[:, 1, 1] = 1j * w * C
320 y[:, 0, 1] = -1j * w * C
321 y[:, 1, 0] = -1j * w * C
322 result.y = y
323 return result
324
325 # Test real and imag port impedance with Y-parameter definition and renormalize()
326 for mismatch_z0 in mismatch_z0_tuple:
327 qucs_ntwk_copy = qucs_ntwk.copy()
328 qucs_ntwk_copy.renormalize(mismatch_z0)
329 skrf_ntwk = self.dummy_media.capacitor(.01e-12, name = name, z0=mismatch_z0)
330 skrf_ntwk_y = capacitor_y_def(self.dummy_media, .01e-12, z0=mismatch_z0)
331 self.assertEqual(qucs_ntwk_copy, skrf_ntwk)
332 assert_array_almost_equal(skrf_ntwk.s, skrf_ntwk_y.s)
333
334 # Config the s_def parameters
335 mismatch_z0 = [25, 50-10j]
336 s_defs = ("power", "pseudo", "traveling")
337
338 # Test different s_def configurations
339 for s_def in s_defs:
340 qucs_ntwk_copy = qucs_ntwk.copy()
341 qucs_ntwk_copy.renormalize(mismatch_z0, s_def=s_def)
342 skrf_ntwk = self.dummy_media.capacitor(.01e-12, name=name, z0=mismatch_z0, s_def=s_def)
343 skrf_ntwk_y = capacitor_y_def(self.dummy_media, .01e-12, z0=mismatch_z0, s_def=s_def)
344 self.assertEqual(skrf_ntwk, qucs_ntwk_copy)
345 self.assertEqual(skrf_ntwk.s_def, qucs_ntwk_copy.s_def)
346 assert_array_almost_equal(skrf_ntwk.s, skrf_ntwk_y.s)
347 self.assertEqual(skrf_ntwk.s_def, skrf_ntwk_y.s_def)
348
349 def test_shunt_capacitor(self):
350 """
351 Test the naming of the network. When circuit is used to connect a
352 topology of networks, they should have unique names.
353 """
354 name = 'shunt_capacitor,p01pF'
355 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
356 skrf_ntwk = self.dummy_media.capacitor(.01e-12, name = name)
357 self.assertEqual(name, skrf_ntwk.name)
358 # test vs analytical ABCD parameters of a shunt capacitor
359 C = 0.1e-12
360 ntwk = self.dummy_media.shunt_capacitor(C)
361 Z = 1/(1j*C*ntwk.frequency.w)
362 ABCD = np.full(ntwk.s.shape, [[1, 0],
363 [-1+1j, 1]])
364 ABCD[:,1,0] = 1/Z
365 assert_array_almost_equal(ABCD, ntwk.a)
366
367 def test_capacitor_q(self):
368 """
369 Compare the component values against s-parameters generated by ADS.
370 Test the naming of the network. When circuit is used to connect a
371 topology of networks, they should have unique names.
372 """
373 name = 'capacitor_q'
374 ads_path = os.path.join(
375 os.path.dirname(os.path.abspath(__file__)),
376 'ads'
377 )
378 ads_ntwk = Network(os.path.join(ads_path, name + '.s2p'))
379 self.dummy_media.frequency = ads_ntwk.frequency
380 skrf_ntwk = self.dummy_media.capacitor_q(C=1.0e-12, f_0=1.0e9, q_factor=30.0, name = name)
381 self.assertEqual(ads_ntwk, skrf_ntwk)
382 self.assertEqual(ads_ntwk.name, skrf_ntwk.name)
383 # test vs analytical ABCD parameters of a series capacitor with q_factor
384 C = 0.1e-12
385 Q = 30.0
386 F = 1.0e9
387 ntwk = self.dummy_media.capacitor_q(C=C, f_0=F, q_factor=Q)
388
389 Z = 1/(2*np.pi*F*C/Q+1j*C*ntwk.frequency.w)
390 ABCD = np.full(ntwk.s.shape, [[1, -1+1j],
391 [0, 1]])
392 ABCD[:,0,1] = Z
393 assert_array_almost_equal(ABCD, ntwk.a)
394
395 def test_inductor(self):
396 """
397 Compare the component values against s-parameters generated by QUCS.
398 Test the naming of the network. When circuit is used to connect a
399 topology of networks, they should have unique names.
400 """
401 name = 'inductor,p1nH'
402 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
403 self.dummy_media.frequency = qucs_ntwk.frequency
404 skrf_ntwk = self.dummy_media.inductor(.1e-9, name = name)
405 self.assertEqual(qucs_ntwk, skrf_ntwk)
406 self.assertEqual(qucs_ntwk.name, skrf_ntwk.name)
407 # test vs analytical ABCD parameters of a series inductor
408 L = 0.1e-9
409 ntwk = self.dummy_media.inductor(L)
410 Z = 1j*L*ntwk.frequency.w
411 ABCD = np.full(ntwk.s.shape, [[1, -1+1j],
412 [0, 1]])
413 ABCD[:,0,1] = Z
414 assert_array_almost_equal(ABCD, ntwk.a)
415
416 def test_inductor_mismatch(self):
417 """
418 Compare the component values against s-parameters generated by QUCS.
419 The mismatch QUCS component is generated by renormalize() method.
420 """
421 # Config the qucs_ntwk and media
422 name = 'inductor,p1nH'
423 qucs_ntwk = Network(os.path.join(self.files_dir, name + '.s2p'))
424 self.dummy_media.frequency = qucs_ntwk.frequency
425 mismatch_z0_tuple = ([25, 50], [25-5j, 50+10j], [25, 50-10j])
426
427 # Test against Y-parameter definition
428 def inductor_y_def(media: DefinedGammaZ0, L, **kwargs):
429 result = media.match(nports=2, **kwargs)
430 w = media.frequency.w
431 y = np.zeros(shape=result.s.shape, dtype=complex)
432 L = np.array(L)
433 y[:, 0, 0] = 1.0 / (1j * w * L)
434 y[:, 1, 1] = 1.0 / (1j * w * L)
435 y[:, 0, 1] = -1.0 / (1j * w * L)
436 y[:, 1, 0] = -1.0 / (1j * w * L)
437 result.y = y
438 return result
439
440 # Test real and imag port impedance with Y-parameter definition and renormalize()
441 for mismatch_z0 in mismatch_z0_tuple:
442 qucs_ntwk_copy = qucs_ntwk.copy()
443 qucs_ntwk_copy.renormalize(mismatch_z0)
444 skrf_ntwk = self.dummy_media.inductor(.1e-9, name = name, z0=mismatch_z0)
445 skrf_ntwk_y = inductor_y_def(self.dummy_media, .1e-9, z0=mismatch_z0)
446 self.assertEqual(qucs_ntwk_copy, skrf_ntwk)
447 assert_array_almost_equal(skrf_ntwk.s, skrf_ntwk_y.s)
448
449 # Config the s_def parameters
450 mismatch_z0 = [25, 50-10j]
451 s_defs = ("power", "pseudo", "traveling")
452
453 # Test different s_def configurations
454 for s_def in s_defs:
455 qucs_ntwk_copy = qucs_ntwk.copy()
456 qucs_ntwk_copy.renormalize(mismatch_z0, s_def=s_def)
457 skrf_ntwk = self.dummy_media.inductor(.1e-9, name=name, z0=mismatch_z0, s_def=s_def)
458 skrf_ntwk_y = inductor_y_def(self.dummy_media, .1e-9, z0=mismatch_z0, s_def=s_def)
459 self.assertEqual(skrf_ntwk, qucs_ntwk_copy)
460 self.assertEqual(skrf_ntwk.s_def, qucs_ntwk_copy.s_def)
461 # numerical precision is slightly reduced when using pseudo s_def
462 assert_array_almost_equal(skrf_ntwk.s, skrf_ntwk_y.s, decimal={'pseudo': 5}.get(s_def, 6))
463 self.assertEqual(skrf_ntwk.s_def, skrf_ntwk_y.s_def)
464
465 def test_shunt_inductor(self):
466 """
467 Test the naming of the network. When circuit is used to connect a
468 topology of networks, they should have unique names.
469 """
470 name = 'shunt_inductor,p1nH'
471 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
472 skrf_ntwk = self.dummy_media.inductor(.1e-9, name = name)
473 self.assertEqual(name, skrf_ntwk.name)
474 # test vs analytical ABCD parameters of a shunt inductor
475 L = 0.1e-9
476 ntwk = self.dummy_media.shunt_inductor(L)
477 Z = 1j*L*ntwk.frequency.w
478 ABCD = np.full(ntwk.s.shape, [[1, 0],
479 [1-1j, 1]])
480 ABCD[:,1,0] = 1/Z
481 assert_array_almost_equal(ABCD, ntwk.a)
482
483 def test_inductor_q(self):
484 """
485 Compare the component values against s-parameters generated by ADS.
486 Test the naming of the network. When circuit is used to connect a
487 topology of networks, they should have unique names.
488 """
489 name = 'inductor_q'
490 ads_path = os.path.join(
491 os.path.dirname(os.path.abspath(__file__)),
492 'ads'
493 )
494 ads_ntwk = Network(os.path.join(ads_path, name + '.s2p'))
495 self.dummy_media.frequency = ads_ntwk.frequency
496 skrf_ntwk = self.dummy_media.inductor_q(L=1.0e-9, f_0=1.0e9, q_factor=30.0, name = name)
497 self.assertEqual(ads_ntwk, skrf_ntwk)
498 self.assertEqual(ads_ntwk.name, skrf_ntwk.name)
499 # test vs analytical ABCD parameters of a series capacitor with q_factor
500 L = 0.1e-9
501 Q = 30.0
502 F = 1.0e9
503 ntwk = self.dummy_media.inductor_q(L=L, f_0=F, q_factor=Q)
504 rdc = F*0.05*(2 * np.pi)*L/Q
505 w_q = 2*np.pi*F
506 rq1 = w_q*L/Q
507 rq2 = np.sqrt(rq1**2-rdc**2)
508 qt = w_q*L/rq2
509 rac = ntwk.frequency.w*L/qt
510
511 Z = np.sqrt(rdc**2+rac**2)+1j*ntwk.frequency.w*L
512 ABCD = np.full(ntwk.s.shape, [[1, -1+1j],
513 [0, 1]])
514 ABCD[:,0,1] = Z
515 assert_array_almost_equal(ABCD, ntwk.a)
516
517 def test_attenuator(self):
518 """
519 Test the naming of the network. When circuit is used to connect a
520 topology of networks, they should have unique names.
521 """
522 name = 'attenuator,-10dB'
523 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
524 skrf_ntwk = self.dummy_media.attenuator(-10, d = 90, unit = 'deg',
525 name = name)
526 self.assertEqual(name, skrf_ntwk.name)
527
528 def test_lossless_mismatch(self):
529 """
530 Test the naming of the network. When circuit is used to connect a
531 topology of networks, they should have unique names.
532 """
533 name = 'lossless_mismatch,-10dB'
534 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
535 skrf_ntwk = self.dummy_media.lossless_mismatch(-10, name = name)
536 self.assertEqual(name, skrf_ntwk.name)
537
538 def test_isolator(self):
539 """
540 Test the naming of the network. When circuit is used to connect a
541 topology of networks, they should have unique names.
542 """
543 name = 'isolator'
544 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
545 skrf_ntwk = self.dummy_media.isolator(name = name)
546 self.assertEqual(name, skrf_ntwk.name)
547
548 def test_line_floating(self):
549 """
550 Test the naming of the network. When circuit is used to connect a
551 topology of networks, they should have unique names.
552 """
553 name = 'floating_line'
554 self.dummy_media.frequency = Frequency(1, 1, 1, unit='GHz')
555 skrf_ntwk = self.dummy_media.line_floating(90, 'deg', name=name)
556 self.assertEqual(name, skrf_ntwk.name)
557
558 # Expected to be a 4-port network
559 self.assertEqual(skrf_ntwk.nports, 4)
560
561 # zero length case
562 s_zero = 1/2 * np.array([[1, 1, 1, -1],
563 [1, 1, -1, 1],
564 [1, -1, 1, 1],
565 [-1, 1, 1, 1]]
566 ).transpose().reshape(-1,4,4)
567 for z0 in [50, 1]:
568 zero_length = self.dummy_media.line_floating(d=0, z0=z0)
569 assert_array_almost_equal(zero_length.s, s_zero)
570
571 def test_line_floating2(self):
572 """
573 Test against line
574 """
575 d = 100
576 unit = 'mm'
577 z0 = 50
578 line = self.dummy_media.line(d, unit, z0)
579 line_floating = self.dummy_media.line_floating(d, unit, z0)
580 gnd = self.dummy_media.short(n_ports=1)
581 line_floating = connect(line_floating, 3, gnd, 0)
582 line_floating = connect(line_floating, 2, gnd, 0)
583 assert_array_almost_equal(line_floating.s, line.s)
584
585 def test_line_floating3(self):
586 """
587 Test against Y-parameter definition
588 """
589 d = 100
590 unit = 'mm'
591
592 def yparam_ref(media, d, unit, z0, s_def=S_DEF_DEFAULT):
593 result = media.match(nports=4, z0=z0, s_def='traveling')
594
595 theta = media.electrical_length(media.to_meters(d=d, unit=unit))
596
597 # From AWR docs on TLINP4.
598 y11 = 1 / (z0 * np.tanh(theta))
599 y12 = -1 / (z0 * np.sinh(theta))
600 y22 = y11
601 y21 = y12
602
603 result.y = \
604 np.array([[ y11, y12, -y11, -y12],
605 [ y21, y22, -y21, -y22],
606 [-y11, -y12, y11, y12],
607 [-y21, -y22, y21, y22]]).transpose(2,0,1)
608 if media.z0_port is not None:
609 result.renormalize(media.z0_port)
610 result.renormalize(result.z0, s_def=s_def)
611 return result
612
613 for z0 in [50, 1, 100+10j]:
614 line = self.dummy_media.line_floating(d, unit, z0)
615 ref_line = yparam_ref(self.dummy_media, d, unit, z0)
616 assert_array_almost_equal(ref_line.s, line.s)
617
618 def test_scalar_gamma_z0_media(self):
619 """
620 test ability to create a Media from scalar quantities for gamma/z0
621 """
622 freq = Frequency(1, 10, 101, unit='GHz')
623 a = DefinedGammaZ0(freq, gamma=1j, z0=50)
624 self.assertEqual(len(freq), len(a))
625 self.assertEqual(len(freq), len(a.gamma))
626 self.assertEqual(len(freq), len(a.z0))
627
628
629 def test_vector_gamma_z0_media(self):
630 """
631 test ability to create a Media from vector quantities for gamma/z0
632 """
633 freq = Frequency(1, 10, 101, unit='GHz')
634 a = DefinedGammaZ0(freq,
635 gamma = 1j*np.ones(len(freq)) ,
636 z0 = 50*np.ones(len(freq)),
637 )
638 self.assertEqual(len(freq), len(a))
639 self.assertEqual(len(freq), len(a.gamma))
640 self.assertEqual(len(freq), len(a.z0))
641
642 def test_write_csv(self):
643 fname = os.path.join(self.files_dir,\
644 'out.csv')
645 self.dummy_media.write_csv(fname)
646 os.remove(fname)
647
648
649 def test_from_csv(self):
650 fname = os.path.join(self.files_dir,\
651 'out.csv')
652 self.dummy_media.write_csv(fname)
653 a_media = DefinedGammaZ0.from_csv(fname)
654 self.assertEqual(a_media,self.dummy_media)
655 os.remove(fname)
656
657
658 class STwoPortsNetworkTestCase(unittest.TestCase):
659 """
660 Check that S parameters of media base elements versus theoretical results.
661 """
662 def setUp(self):
663 self.dummy_media = DefinedGammaZ0(
664 frequency=Frequency(1, 100, 21, unit='GHz'),
665 gamma=1j,
666 z0=50,
667 )
668
669 def test_s_series_element(self):
670 """
671 Series elements of impedance Z:
672
673 ○---[Z]---○
674
675 ○---------○
676
677 have S matrix of the form:
678 [ Z/Z0 / (Z/Z0 + 2) 2/(Z/Z0 + 2) ]
679 [ 2/(Z/Z0 + 2) Z/Z0 / (Z/Z0 + 2) ]
680 """
681 R = 1.0 # Ohm
682 ntw = self.dummy_media.resistor(R)
683 Z0 = self.dummy_media.z0
684 S11 = (R/Z0) / (R/Z0 + 2)
685 S21 = 2 / (R/Z0 + 2)
686 assert_array_almost_equal(ntw.s[:,0,0], S11)
687 assert_array_almost_equal(ntw.s[:,0,1], S21)
688 assert_array_almost_equal(ntw.s[:,1,0], S21)
689 assert_array_almost_equal(ntw.s[:,1,1], S11)
690
691 def test_s_shunt_element(self):
692 """
693 Shunt elements of admittance Y:
694
695 ○---------○
696 |
697 [Y]
698 |
699 ○---------○
700
701 have S matrix of the form:
702 [ -Y Z0 / (Y Z0 + 2) 2/(Y Z0 + 2) ]
703 [ 2/(Y Z0 + 2) Z/Z0 / (Y Z0 + 2) ]
704 """
705 R = 1.0 # Ohm
706 ntw = self.dummy_media.shunt(self.dummy_media.resistor(R)**self.dummy_media.short())
707 Z0 = self.dummy_media.z0
708 S11 = -(1/R*Z0) / (1/R*Z0 + 2)
709 S21 = 2 / (1/R*Z0 + 2)
710 assert_array_almost_equal(ntw.s[:,0,0], S11)
711 assert_array_almost_equal(ntw.s[:,0,1], S21)
712 assert_array_almost_equal(ntw.s[:,1,0], S21)
713 assert_array_almost_equal(ntw.s[:,1,1], S11)
714
715 def test_s_lossless_line(self):
716 """
717 Lossless transmission line of characteristic impedance z1, length l
718 and wavenumber beta
719 _______
720 ○----- -----○
721 z0 z1 z0
722 ○-----_______-----○
723
724 """
725 l = 5.0
726 z1 = 30.0
727 z0 = self.dummy_media.z0
728
729 ntw = self.dummy_media.line(d=0, unit='m', z0=z0) \
730 ** self.dummy_media.line(d=l, unit='m', z0=z1) \
731 ** self.dummy_media.line(d=0, unit='m', z0=z0)
732
733 beta = self.dummy_media.beta
734 _z1 = z1/z0
735 S11 = 1j*(_z1**2 - 1)*np.sin(beta*l) / \
736 (2*_z1*np.cos(beta*l) + 1j*(_z1**2 + 1)*np.sin(beta*l))
737 S21 = 2*_z1 / \
738 (2*_z1*np.cos(beta*l) + 1j*(_z1**2 + 1)*np.sin(beta*l))
739 assert_array_almost_equal(ntw.s[:,0,0], S11)
740 assert_array_almost_equal(ntw.s[:,0,1], S21)
741 assert_array_almost_equal(ntw.s[:,1,0], S21)
742 assert_array_almost_equal(ntw.s[:,1,1], S11)
743
744 def test_s_lossy_line(self):
745 """
746 Lossy transmission line of characteristic impedance Z0, length l
747 and propagation constant gamma = alpha + j beta
748
749 ○---------○
750
751 ○---------○
752
753 has ABCD matrix of the form:
754
755 [ cosh(gamma l) Z0 sinh(gamma l) ]
756 [ 1/Z0 sinh(gamma l) cosh(gamma l) ]
757 """
758
759
760 class ABCDTwoPortsNetworkTestCase(unittest.TestCase):
761 """
762 Check that ABCD parameters of media base elements (such as lumped elements)
763 versus theoretical results.
764 """
765 def setUp(self):
766 self.dummy_media = DefinedGammaZ0(
767 frequency=Frequency(1, 100, 21, unit='GHz'),
768 gamma=1j,
769 z0=50 ,
770 )
771
772 def test_abcd_series_element(self):
773 """
774 Series elements of impedance Z:
775
776 ○---[Z]---○
777
778 ○---------○
779
780 have ABCD matrix of the form:
781 [ 1 Z ]
782 [ 0 1 ]
783 """
784 R = 1.0 # Ohm
785 ntw = self.dummy_media.resistor(R)
786 assert_array_almost_equal(ntw.a[:,0,0], 1.0)
787 assert_array_almost_equal(ntw.a[:,0,1], R)
788 assert_array_almost_equal(ntw.a[:,1,0], 0.0)
789 assert_array_almost_equal(ntw.a[:,1,1], 1.0)
790
791 def test_abcd_shunt_element(self):
792 """
793 Shunt elements of admittance Y:
794
795 ○---------○
796 |
797 [Y]
798 |
799 ○---------○
800
801 have ABCD matrix of the form:
802 [ 1 0 ]
803 [ Y 1 ]
804 """
805 R = 1.0 # Ohm
806 ntw = self.dummy_media.shunt(self.dummy_media.resistor(R)**self.dummy_media.short())
807 assert_array_almost_equal(ntw.a[:,0,0], 1.0)
808 assert_array_almost_equal(ntw.a[:,0,1], 0.0)
809 assert_array_almost_equal(ntw.a[:,1,0], 1.0/R)
810 assert_array_almost_equal(ntw.a[:,1,1], 1.0)
811
812 def test_abcd_series_shunt_elements(self):
813 """
814 Series and Shunt elements of impedance Zs and Zp:
815
816 ○---[Zs]--------○
817 |
818 [Zp]
819 |
820 ○--------------○
821 have ABCD matrix of the form:
822 [ 1 + Zs/Zp Zs ]
823 [ 1/Zp 1 ]
824 """
825 Rs = 2.0
826 Rp = 3.0
827 serie_resistor = self.dummy_media.resistor(Rs)
828 shunt_resistor = self.dummy_media.shunt(self.dummy_media.resistor(Rp) ** self.dummy_media.short())
829
830 ntw = serie_resistor ** shunt_resistor
831
832 assert_array_almost_equal(ntw.a[:,0,0], 1.0+Rs/Rp)
833 assert_array_almost_equal(ntw.a[:,0,1], Rs)
834 assert_array_almost_equal(ntw.a[:,1,0], 1.0/Rp)
835 assert_array_almost_equal(ntw.a[:,1,1], 1.0)
836
837 def test_abcd_thru(self):
838 """
839 Thru has ABCD matrix of the form:
840 [ 1 0 ]
841 [ 0 1 ]
842 """
843 ntw = self.dummy_media.thru()
844 assert_array_almost_equal(ntw.a[:,0,0], 1.0)
845 assert_array_almost_equal(ntw.a[:,0,1], 0.0)
846 assert_array_almost_equal(ntw.a[:,1,0], 0.0)
847 assert_array_almost_equal(ntw.a[:,1,1], 1.0)
848
849 def test_abcd_lossless_line(self):
850 """
851 Lossless transmission line of characteristic impedance Z0, length l
852 and wavenumber beta
853
854 ○---------○
855
856 ○---------○
857
858 has ABCD matrix of the form:
859
860 [ cos(beta l) j Z0 sin(beta l) ]
861 [ j/Z0 sin(beta l) cos(beta l) ]
862 """
863 l = 5
864 z0 = 80
865 ntw = self.dummy_media.line(d=l, unit='m', z0=z0)
866 beta = self.dummy_media.beta
867 assert_array_almost_equal(ntw.a[:,0,0], np.cos(beta*l))
868 assert_array_almost_equal(ntw.a[:,0,1], 1j*z0*np.sin(beta*l))
869 assert_array_almost_equal(ntw.a[:,1,0], 1j/z0*np.sin(beta*l))
870 assert_array_almost_equal(ntw.a[:,1,1], np.cos(beta*l))
871
872 def test_abcd_lossy_line(self):
873 """
874 Lossy transmission line of characteristic impedance Z0, length l
875 and propagation constant gamma = alpha + j beta
876
877 ○---------○
878
879 ○---------○
880
881 has ABCD matrix of the form:
882
883 [ cosh(gamma l) Z0 sinh(gamma l) ]
884 [ 1/Z0 sinh(gamma l) cosh(gamma l) ]
885 """
886 l = 5.0
887 z0 = 30.0
888 alpha = 0.5
889 beta = 2.0
890 lossy_media = DefinedGammaZ0(
891 frequency=Frequency(1, 100, 21, unit='GHz'),
892 gamma=alpha + 1j*beta,
893 z0=z0
894 )
895 ntw = lossy_media.line(d=l, unit='m', z0=z0)
896 gamma = lossy_media.gamma
897 assert_array_almost_equal(ntw.a[:,0,0], np.cosh(gamma*l))
898 assert_array_almost_equal(ntw.a[:,0,1], z0*np.sinh(gamma*l))
899 assert_array_almost_equal(ntw.a[:,1,0], 1.0/z0*np.sinh(gamma*l))
900 assert_array_almost_equal(ntw.a[:,1,1], np.cosh(gamma*l))
901
902 class DefinedGammaZ0_s_def(unittest.TestCase):
903 """Test various media constructs with complex ports and different s_def"""
904
905 def test_complex_ports(self):
906 m = DefinedGammaZ0(
907 frequency = Frequency(1, 1, 1, unit='ghz'),
908 gamma=1j,
909 z0_port = 50,
910 z0 = 10+20j,
911 )
912 self.assertTrue(m.z0.imag != 0)
913
914 # Powerwave short is -Z0.conj()/Z0
915 short = m.short(z0=m.z0, s_def='power')
916 self.assertTrue(short.s != -1)
917 # Should be -1 when converted to traveling s_def
918 np.testing.assert_allclose(short.s_traveling, -1)
919
920 short = m.short(z0=m.z0, s_def='traveling')
921 np.testing.assert_allclose(short.s, -1)
922
923 short = m.short(z0=m.z0, s_def='pseudo')
924 np.testing.assert_allclose(short.s, -1)
925
926 # Mismatches agree with real port impedances
927 mismatch_traveling = m.impedance_mismatch(z1=10, z2=50, s_def='traveling')
928 mismatch_pseudo = m.impedance_mismatch(z1=10, z2=50, s_def='pseudo')
929 mismatch_power = m.impedance_mismatch(z1=10, z2=50, s_def='power')
930 np.testing.assert_allclose(mismatch_traveling.s, mismatch_pseudo.s)
931 np.testing.assert_allclose(mismatch_traveling.s, mismatch_power.s)
932
933 mismatch_traveling = m.impedance_mismatch(z1=10+10j, z2=50-20j, s_def='traveling')
934 mismatch_pseudo = m.impedance_mismatch(z1=10+10j, z2=50-20j, s_def='pseudo')
935 mismatch_power = m.impedance_mismatch(z1=10+10j, z2=50-20j, s_def='power')
936
937 # Converting thru to new impedance should give impedance mismatch.
938 # The problem is that thru Z-parameters have infinities
939 # and renormalization goes through Z-parameters making
940 # it very inaccurate.
941 thru_traveling = m.thru(s_def='traveling')
942 thru_traveling.renormalize(z_new=[10+10j,50-20j])
943 thru_pseudo = m.thru(s_def='pseudo')
944 thru_pseudo.renormalize(z_new=[10+10j,50-20j])
945 thru_power = m.thru(s_def='power')
946 thru_power.renormalize(z_new=[10+10j,50-20j])
947
948 np.testing.assert_allclose(thru_traveling.s, mismatch_traveling.s, rtol=1e-3)
949 np.testing.assert_allclose(thru_pseudo.s, mismatch_pseudo.s, rtol=1e-3)
950 np.testing.assert_allclose(thru_power.s, mismatch_power.s, rtol=1e-3)
951
952 if __name__ == '__main__':
953 unittest.main()