Debian Codegraph @

skrf/media/tests/test_definedaeptandz0.py

raw symbols SCIP
1 import os
2 import unittest
3
4 import numpy as np
5
6 import skrf as rf
7 from skrf.media import DefinedAEpTandZ0
8
9 try:
10 from matplotlib import pyplot as plt
11 rf.stylely()
12 except ImportError:
13 pass
14
15
16
17 class DefinedAEpTandZ0TestCase(unittest.TestCase):
18 """
19 Testcase for the DefinedAEpTandZ0 Media
20 """
21 def setUp(self):
22 """
23 Read in all the network data required for tests
24 """
25 self.data_dir_awr = os.path.dirname(os.path.abspath(__file__)) + \
26 '/awr/'
27
28 self.ref_awr = [
29 {'color': 'k',
30 'n': rf.Network(os.path.join(self.data_dir_awr,
31 'tlinp.s2p'))},
32 ]
33
34 # default parameter set for tests
35 self.verbose = False # output comparison plots if True
36 self.Zn = 41.0635803351402
37 self.A = 0.0670188722315605
38 self.l = 74.2415154883262e-3
39 self.ep_r = 3.25354286428265
40 self.tand = 0.0133936758939493
41 # frequency vector with only 3 points
42 self.freq = rf.Frequency(1, 3, 3, 'GHz')
43
44
45 def test_line_awr(self):
46 """
47 Test against the AWR results
48 """
49 if self.verbose:
50 fig, axs = plt.subplots(2, 2, figsize = (8,6))
51 fig.suptitle('awr/skrf')
52 fig2, axs2 = plt.subplots(2, 2, figsize = (8,6))
53 fig2.suptitle('awr/skrf residuals')
54
55 limit_db = 1e-5
56 limit_deg = 1e-4
57
58 for ref in self.ref_awr:
59 m = DefinedAEpTandZ0(
60 frequency = ref['n'].frequency,
61 z0 = self.Zn,
62 ep_r = self.ep_r,
63 A = self.A,
64 f_A = ref['n'].frequency.f[0],
65 tanD = self.tand,
66 z0_port = 50
67 )
68 line = m.line(d=self.l, unit='m')
69 line.name = 'skrf,awr'
70
71 # residuals
72 res = line / ref['n']
73 res.name = 'residuals ' + ref['n'].name
74
75 # test if within limit lines
76 self.assertTrue(
77 np.all(np.abs(res.s_db[:, 0, 0]) < limit_db))
78 self.assertTrue(
79 np.all(np.abs(res.s_deg[:, 0, 0]) < limit_deg))
80 self.assertTrue(np.all(np.abs(res.s_db[:, 1, 0]) < limit_db))
81 self.assertTrue(np.all(np.abs(res.s_deg[:, 1, 0]) < limit_deg))
82
83 if self.verbose:
84 line.plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'],
85 linestyle = 'none', marker = 'x')
86 ref['n'].plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'])
87 res.plot_s_db(0, 0, ax = axs2[0, 0], linestyle = 'dashed',
88 color = ref['color'])
89
90 line.plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'],
91 linestyle = 'none', marker = 'x')
92 ref['n'].plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'])
93 res.plot_s_deg(0, 0, ax = axs2[0, 1], linestyle = 'dashed',
94 color = ref['color'])
95
96 line.plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'],
97 linestyle = 'none', marker = 'x')
98 ref['n'].plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'])
99 res.plot_s_db(1, 0, ax = axs2[1, 0], linestyle = 'dashed',
100 color = ref['color'])
101
102 line.plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'],
103 linestyle = 'none', marker = 'x')
104 ref['n'].plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'])
105 res.plot_s_deg(1, 0, ax = axs2[1, 1], linestyle = 'dashed',
106 color = ref['color'])
107
108
109 if self.verbose:
110 fig.tight_layout()
111 fig2.tight_layout()
112
113 def test_nominal_impedance_dispersion(self):
114 """
115 Test if passing scalar value to z0 results to this nominal impedance
116 being dispersed to a complex frequency-dependent characteristic
117 impedance.
118 """
119 Zn = 75
120 m = DefinedAEpTandZ0(
121 frequency = self.freq,
122 z0 = Zn,
123 ep_r = self.ep_r,
124 A = self.A,
125 f_A = self.freq.f[0],
126 tanD = self.tand,
127 z0_port = 50
128 )
129 # z0_characteristic complex and frequency dependent
130 self.assertTrue(np.all(np.abs(np.imag(m.z0_characteristic) > 1e-1)))
131 self.assertTrue(m.z0_characteristic[0] != m.z0_characteristic[-1])
132
133 def test_raw_z0_array(self):
134 """
135 Test if passing array-like value to z0 results to this raw value being
136 assigned to characteristic impedance.
137 """
138
139 # Single-element array are kept as raw z0_characteristic
140 z1 = [75]
141 m1 = DefinedAEpTandZ0(
142 frequency = self.freq,
143 z0 = z1,
144 ep_r = self.ep_r,
145 A = self.A,
146 f_A = self.freq.f[0],
147 tanD = self.tand,
148 z0_port = 50
149 )
150 self.assertTrue(np.allclose(z1 * np.ones(3), m1.z0_characteristic))
151 # Sequence are kept as raw z0_characteristic
152 z2 = [45, 50, 55]
153 m2 = DefinedAEpTandZ0(
154 frequency = self.freq,
155 z0 = z2,
156 ep_r = self.ep_r,
157 A = self.A,
158 f_A = self.freq.f[0],
159 tanD = self.tand,
160 z0_port = 50
161 )
162 self.assertTrue(np.allclose(z2, m2.z0_characteristic))
163 # Numpy arrays are kept as raw z0_characteristic
164 z3 = np.array([48, 50, 52])
165 m3 = DefinedAEpTandZ0(
166 frequency = self.freq,
167 z0 = z3,
168 ep_r = self.ep_r,
169 A = self.A,
170 f_A = self.freq.f[0],
171 tanD = self.tand,
172 z0_port = 50
173 )
174 self.assertTrue(np.allclose(z3, m3.z0_characteristic))
175
176 if __name__ == '__main__':
177 unittest.main()