Debian Codegraph @

skrf/media/tests/test_cpw.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 import skrf as rf
8 from skrf.frequency import Frequency
9 from skrf.media import CPW
10
11 try:
12 from matplotlib import pyplot as plt
13 rf.stylely()
14 except ImportError:
15 pass
16
17
18
19
20 class CPWTestCase(unittest.TestCase):
21 def setUp(self):
22 self.data_dir_qucs = os.path.join(
23 os.path.dirname(os.path.abspath(__file__)),
24 'qucs_prj')
25 self.data_dir_ads = os.path.join(
26 os.path.dirname(os.path.abspath(__file__)),
27 'ads')
28
29 fname = os.path.join(self.data_dir_qucs, 'cpw.s2p')
30 self.qucs_ntwk = rf.Network(fname)
31
32 # create various examples
33 self.freq = rf.Frequency(start=1, stop=20, npoints=21, unit='GHz')
34 # infinite quarz substrate, infinitely thin metal
35 self.cpw1 = CPW(frequency=self.freq, w=40e-6, s=20e-6, h = 100e-3, ep_r=3.78)
36 # infinite GaAs substrate, infinitely thin metal
37 self.cpw2 = CPW(frequency=self.freq, w=75e-6, s=50e-6, h = 100e-3, ep_r=12.9)
38
39 # coplanar on FR-4 printed circuit board without conductor backing
40 # with zero thickness strip
41 self.cpw4 = CPW(frequency = self.freq, w = 3.0e-3, s = 0.3e-3,
42 t = None, ep_r = 4.5, rho = None, z0_port = 50.)
43 self.cpw5 = CPW(frequency = self.freq, w = 3.0e-3, s = 0.3e-3,
44 t = 0., ep_r = 4.5, rho = None, z0_port = 50.)
45
46 # more newtorks to test against Qucs with air or metal backing
47 self.ref_qucs = [
48 {'has_metal_backside': True, 'w': 1.6e-3, 's': 0.3e-3, 't': 35e-6,
49 'h': 1.55e-3, 'color': 'b',
50 'n': rf.Network(os.path.join(self.data_dir_qucs,
51 'cpw,t=35um,w=1.6mm,s=0.3mm,l=25mm,backside=metal.s2p'))},
52 {'has_metal_backside': False, 'w': 3.0e-3, 's': 0.3e-3, 't': 35e-6,
53 'h': 1.55e-3, 'color': 'g',
54 'n': rf.Network(os.path.join(self.data_dir_qucs,
55 'cpw,t=35um,w=3mm,s=0.3mm,l=25mm,backside=air.s2p'))},
56 {'has_metal_backside': False, 'w': 3.0e-3, 's': 0.3e-3, 't': 0,
57 'h': 100e-3, 'color': 'r',
58 'n': rf.Network(os.path.join(self.data_dir_qucs,
59 'cpw,t=0,h=100mm,w=3mm,s=0.3mm,l=25mm,backside=air.s2p'))},
60 ]
61
62 self.ref_ads = [
63 {'has_metal_backside': False, 'w': 3.0e-3, 's': 0.3e-3, 't': 0.,
64 'h': 1.55e-3, 'color': 'C0',
65 'n': rf.Network(os.path.join(self.data_dir_ads,
66 'cpw,t=0um.s2p'))},
67 {'has_metal_backside': True, 'w': 1.6e-3, 's': 0.3e-3, 't': 0.,
68 'h': 1.55e-3, 'color': 'C1',
69 'n': rf.Network(os.path.join(self.data_dir_ads,
70 'cpwg,t=0um.s2p'))},
71 ]
72
73 # these would fail comparison because ADS use another strip thickness
74 # correction. Kept for reference in case of future work.
75 self.ref_ads_failling = [
76 {'has_metal_backside': False, 'w': 3.0e-3, 's': 0.3e-3, 't': 35e-6,
77 'h': 1.55e-3, 'color': 'C2',
78 'n': rf.Network(os.path.join(self.data_dir_ads,
79 'cpw,t=35um.s2p'))},
80 {'has_metal_backside': True, 'w': 1.6e-3, 's': 0.3e-3, 't': 35e-6,
81 'h': 1.55e-3, 'color': 'C3',
82 'n': rf.Network(os.path.join(self.data_dir_ads,
83 'cpwg,t=35um.s2p'))},
84 ]
85
86 # default parameter set for tests
87 self.verbose = False # output comparison plots if True
88 self.l = 25e-3
89 self.ep_r = 4.5
90 self.tand = 0.018
91 self.rho = 1.7e-8
92
93 def test_qucs_network(self):
94 """
95 Test against the Qucs project results
96 """
97 if self.verbose:
98 fig, axs = plt.subplots(2, 2, figsize = (8,6))
99 fig.suptitle('qucs/skrf')
100 fig2, axs2 = plt.subplots(2, 2, figsize = (8,6))
101 fig2.suptitle('qucs/skrf residuals')
102 fig3, ax3 = plt.subplots(1, 1, figsize = (8,3))
103
104 limit = 2e-3
105
106 for ref in self.ref_qucs:
107 cpw = CPW(frequency = ref['n'].frequency, z0_port = 50.,
108 w = ref['w'], s = ref['s'], t = ref['t'],
109 h = ref['h'],
110 has_metal_backside = ref['has_metal_backside'],
111 ep_r = self.ep_r, rho = self.rho,
112 tand = self.tand,
113 compatibility_mode = 'qucs',
114 diel = 'frequencyinvariant')
115 line = cpw.line(d=self.l, unit='m')
116 line.name = '`Media.CPW` skrf,qucs'
117
118 # residuals
119 res = line - ref['n']
120
121 # test if within limit
122 self.assertTrue(np.all(np.abs(res.s) < limit))
123
124 if self.verbose:
125 ax3.plot(np.abs(res.s[0,0]))
126 ax3.plot(np.abs(res.s[0,1]))
127 ax3.plot(np.abs(res.s[1,0]))
128 ax3.plot(np.abs(res.s[1,1]))
129 res = line / ref['n']
130 res.name = 'residuals ' + ref['n'].name
131 line.plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'],
132 linestyle = 'none', marker = 'x')
133 ref['n'].plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'])
134 res.plot_s_db(0, 0, ax = axs2[0, 0], linestyle = 'dashed',
135 color = ref['color'])
136
137 line.plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'],
138 linestyle = 'none', marker = 'x')
139 ref['n'].plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'])
140 res.plot_s_deg(0, 0, ax = axs2[0, 1], linestyle = 'dashed',
141 color = ref['color'])
142
143 line.plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'],
144 linestyle = 'none', marker = 'x')
145 ref['n'].plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'])
146 res.plot_s_db(1, 0, ax = axs2[1, 0], linestyle = 'dashed',
147 color = ref['color'])
148
149 line.plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'],
150 linestyle = 'none', marker = 'x')
151 ref['n'].plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'])
152 res.plot_s_deg(1, 0, ax = axs2[1, 1], linestyle = 'dashed',
153 color = ref['color'])
154
155
156 if self.verbose:
157 axs[1, 0].legend(prop={'size': 6})
158 axs[0, 0].get_legend().remove()
159 axs[0, 1].get_legend().remove()
160 axs[1, 1].get_legend().remove()
161 fig.tight_layout()
162
163 axs2[1, 0].legend(prop={'size': 6})
164 axs2[0, 0].get_legend().remove()
165 axs2[0, 1].get_legend().remove()
166 axs2[1, 1].get_legend().remove()
167 fig2.tight_layout()
168
169 def test_ads_network(self):
170 """
171 Test against the ADS simulator results
172 """
173 if self.verbose:
174 fig, axs = plt.subplots(2, 2, figsize = (8,6))
175 fig.suptitle('ads/skrf')
176 fig2, axs2 = plt.subplots(2, 2, figsize = (8,6))
177 fig2.suptitle('ads/skrf residuals')
178 fig3, ax3 = plt.subplots(1, 1, figsize = (8,3))
179
180 limit = 1e-3
181
182 for ref in self.ref_ads:
183 cpw = CPW(frequency = ref['n'].frequency, z0_port = 50.,
184 w = ref['w'], s = ref['s'], t = ref['t'],
185 h = ref['h'],
186 has_metal_backside = ref['has_metal_backside'],
187 ep_r = self.ep_r, rho = self.rho,
188 tand = self.tand,
189 compatibility_mode = 'ads',
190 diel = 'djordjevicsvensson')
191 line = cpw.line(d=self.l, unit='m')
192 line.name = '`Media.CPW` skrf,ads'
193
194 # residuals
195 res = line - ref['n']
196
197 # test if within limit
198 self.assertTrue(np.all(np.abs(res.s) < limit))
199
200 if self.verbose:
201 ax3.plot(np.abs(res.s[0,0]))
202 ax3.plot(np.abs(res.s[0,1]))
203 ax3.plot(np.abs(res.s[1,0]))
204 ax3.plot(np.abs(res.s[1,1]))
205 res = line / ref['n']
206 res.name = 'residuals ' + ref['n'].name
207 line.plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'],
208 linestyle = 'none', marker = 'x')
209 ref['n'].plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'])
210 res.plot_s_db(0, 0, ax = axs2[0, 0], linestyle = 'dashed',
211 color = ref['color'])
212
213 line.plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'],
214 linestyle = 'none', marker = 'x')
215 ref['n'].plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'])
216 res.plot_s_deg(0, 0, ax = axs2[0, 1], linestyle = 'dashed',
217 color = ref['color'])
218
219 line.plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'],
220 linestyle = 'none', marker = 'x')
221 ref['n'].plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'])
222 res.plot_s_db(1, 0, ax = axs2[1, 0], linestyle = 'dashed',
223 color = ref['color'])
224
225 line.plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'],
226 linestyle = 'none', marker = 'x')
227 ref['n'].plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'])
228 res.plot_s_deg(1, 0, ax = axs2[1, 1], linestyle = 'dashed',
229 color = ref['color'])
230
231
232 if self.verbose:
233 axs[1, 0].legend(prop={'size': 6})
234 axs[0, 0].get_legend().remove()
235 axs[0, 1].get_legend().remove()
236 axs[1, 1].get_legend().remove()
237 fig.tight_layout()
238
239 axs2[1, 0].legend(prop={'size': 6})
240 axs2[0, 0].get_legend().remove()
241 axs2[0, 1].get_legend().remove()
242 axs2[1, 1].get_legend().remove()
243 fig2.tight_layout()
244
245 def test_z0(self):
246 """
247 Test the CPW Characteristic Impedances
248
249 Values from http://wcalc.sourceforge.net/cgi-bin/coplanar.cgi
250 """
251 # values from http://wcalc.sourceforge.net/cgi-bin/coplanar.cgi
252 assert_array_almost_equal(self.cpw1.z0, 77.93, decimal=2)
253
254 def test_ep_reff(self):
255 """
256 Test the effective permittivity of CPW
257 """
258 # values from https://www.microwaves101.com/calculators/864-coplanar-waveguide-calculator
259 assert_array_almost_equal(self.cpw1.ep_reff, 2.39, decimal=2)
260 assert_array_almost_equal(self.cpw2.ep_reff, 6.94, decimal=2)
261
262 def test_z0_vs_f(self):
263 """
264 Test the CPW Characteristic Impedance vs frequency.
265
266 Reference data comes from Qucs Documentation (Fig 12.2)
267 """
268 w_over_s_qucs, z0_qucs = np.loadtxt(
269 os.path.join(self.data_dir_qucs, 'cpw_qucs_ep_r9dot5.csv'),
270 delimiter=';', unpack=True)
271
272 w = 1
273 z0 = []
274 for w_o_s in w_over_s_qucs:
275 # simulate infinite thickness by providing h >> w
276 _cpw = CPW(frequency=self.freq[0], w=w, s=w/w_o_s, h=1e9, ep_r=9.5)
277 z0.append(_cpw.z0[0].real)
278
279 # all to a 3% relative difference
280 # this is quite a large discrepancy, but I extracted the ref values from the plot
281 # one could do better eventually by extracting values from Qucs directly
282 rel_diff = (z0_qucs-np.array(z0))/z0_qucs
283 self.assertTrue(np.all(np.abs(rel_diff) < 0.03))
284
285 def test_alpha_warning(self):
286 """
287 Test if alpha_conductor warns when t < 3 * skin_depth
288 """
289 # cpw line on 1.5mm FR-4 substrate
290 freq = Frequency(1, 1, 1, 'MHz')
291 with self.assertWarns(RuntimeWarning) as context:
292 cpw = CPW(frequency = freq, z0_port = 50., w = 3.0e-3, s = 0.3e-3, t = 35e-6,
293 ep_r = 4.5, rho = 1.7e-8)
294
295 line = cpw.line(d = 25e-3, unit = 'm')
296
297 def test_zero_thickness(self):
298 """
299 Test if alpha_conductor is nullified when thickness = 0. or None
300 """
301 assert_array_almost_equal(self.cpw4.alpha_conductor, 0.00, decimal=6)
302 assert_array_almost_equal(self.cpw5.alpha_conductor, 0.00, decimal=6)