11"""Tests for cavity sub-module."""
22
3+ import os
34import numpy as np
45import scipy .constants as sc
56from pytest import approx
7+ import matplotlib .pyplot as plt
8+ import skrf as rf
69
710import waveguide as wg
811
@@ -34,7 +37,7 @@ def test_example_6p3():
3437
3538 # Mode numbers
3639 m , n = 1 , 0
37- l = 1
40+ ell = 1
3841
3942 # Dielectric properties of polyethylene
4043 er_mag = 2.25
@@ -49,9 +52,13 @@ def test_example_6p3():
4952 assert k == approx (157.08 , abs = 1 )
5053
5154 # Resonant frequency
52- fres = wg .resonant_frequency (a , b , d , m = m , n = n , l = l , er = er_mag , ur = 1 )
55+ fres = wg .resonant_frequency (a , b , d , m = m , n = n , l = ell , er = er_mag , ur = 1 )
5356 assert fres == approx (f , abs = 0.02 * sc .giga )
5457
58+ # Recover permittivity
59+ er_mag_recovered = wg .resonant_frequency2permittivity (ell , fres , a , b , d )
60+ assert er_mag_recovered == approx (er_mag , abs = 0.02 )
61+
5562 # Intrinsic impedance
5663 eta = wg .intrinsic_impedance (er = er_mag , ur = ur )
5764 assert eta == approx (251.3 , abs = 0.2 )
@@ -70,6 +77,22 @@ def test_example_6p3():
7077 q_net = wg .qfactor_parallel (qc , qd )
7178 assert q_net == approx (1927 , abs = 3 )
7279
80+ # De-embed Q-factor
81+ qc_recovered = wg .deembed_qfactor (q_net , qd )
82+ assert qc_recovered == approx (qc , abs = 10 )
83+
84+ # Recover surface resistance
85+ rs_recovered = wg .q2surface_resistance (fres , qc , a , b , d , l = ell , er = er_mag , ur = ur )
86+ assert rs_recovered == approx (rs , abs = 2e-4 )
87+
88+ # Recover conductivity
89+ cond_recovered = wg .q2conductivity (fres , qc , a , b , d , l = ell , er = er_mag , ur = ur )
90+ assert cond_recovered == approx (cond , 2e4 )
91+
92+ # Recover loss tangent
93+ tand_recovered = wg .q2loss_tangent (qd )
94+ assert tand_recovered == approx (tand , abs = 2e-4 )
95+
7396
7497def test_problem_6p9 ():
7598
@@ -102,11 +125,150 @@ def test_problem_6p9():
102125 assert q102 == approx (7987 , abs = 10 )
103126
104127
105- # TODO: add problem 6.23 from Pozar
128+ def test_problem_6p23 ():
129+ """Test problem 6.23 from Pozar."""
130+
131+ fres = 9 * sc .giga
132+ q = 11_000
133+ a , b = 2.5 * sc .centi , 1.25 * sc .centi
134+
135+ # Wavenumber
136+ k0 = wg .wavenumber (fres , er = 1 , ur = 1 )
137+ assert k0 == approx (188 , abs = 5 )
138+
139+ # Phase constant
140+ beta0 = wg .phase_constant (fres , a , b , er = 1 , ur = 1 , m = 1 , n = 0 )
141+ assert beta0 == approx (140.5 , abs = 0.5 )
142+
143+ # Length
144+ length = np .pi / beta0
145+ assert length == approx (2.24 * sc .centi , abs = 0.05 * sc .centi )
146+
147+
148+ def test_simulated_cavity (debug = False ):
149+
150+ # Dimensions
151+ a , b , d = 280 * sc .mil , 140 * sc .mil , 6 * sc .inch
152+ cond = 1e7
153+
154+ # Load simulated data
155+ filename = os .path .join ('data' , 'cavity.s2p' )
156+ dir_name = os .path .dirname (__file__ )
157+ filename = os .path .join (dir_name , filename )
158+ data = rf .Network (filename )
159+
160+ # Unpack
161+ f = data .f
162+ s21 = data .s [:, 1 , 0 ]
163+
164+ # Find resonances
165+ fres_list = wg .find_resonances (f , wg .db20 (s21 ), height = - 90 )
166+
167+ # Get Q-factor
168+ fres , q0 , ql , _ = wg .find_qfactor (f , np .abs (s21 ), fres_list , fspan = 5e7 , ncol = 6 , figsize = (14 ,8 ), debug = debug )
169+
170+ # Resonant frequencies (theory)
171+ ell_start = 3
172+ ell = np .arange (ell_start , ell_start + len (fres ))
173+ fres_theory = wg .resonant_frequency (a , b , d , l = ell )
174+ np .testing .assert_almost_equal (fres / 1e9 , fres_theory / 1e9 , decimal = 1 )
175+
176+ # Q-factor (theory)
177+ qc_theory = wg .qfactor_conduction (a , b , d , cond , l = ell )
178+
179+ # Plot Q-factor
180+ if debug :
181+ plt .figure ()
182+ plt .plot (ell , qc_theory , 'ko--' , label = 'Theory' )
183+ plt .plot (ell , q0 , 'ro--' , label = "Q-factor (corrected)" )
184+ plt .plot (ell , ql , 'bo--' , label = "Q-factor (loaded)" )
185+ plt .xlim (xmin = 0 )
186+ plt .legend ()
187+ plt .show ()
188+
189+ # Get conductivity from Q-factor
190+ cond_q = wg .q2conductivity (fres , q0 , a , b , d , l = ell )
191+ cond_theory = wg .q2conductivity (fres_theory , qc_theory , a , b , d , l = ell )
192+
193+ # Plot conductivity
194+ if debug :
195+ plt .figure ()
196+ plt .plot (fres , cond_q , 'bo-' , label = "From Q-factor" )
197+ plt .plot (fres_theory , cond_theory , 'ro--' , label = "From theory" )
198+ plt .axhline (cond , c = 'k' , ls = '--' )
199+ plt .ylabel ("Conductivity (S/m)" )
200+ plt .xlabel ("Frequency (GHz)" )
201+ plt .show ()
202+
203+ # Test
204+ np .testing .assert_allclose (cond_q , cond * np .ones_like (cond_q ), atol = 1e6 )
205+ np .testing .assert_allclose (cond_theory , cond * np .ones_like (cond_q ), atol = 1 )
206+
207+
208+ # def test_simulated_cavity_with_hdpe(debug=False):
209+ #
210+ # # Dimensions
211+ # a, b, d = 280*sc.mil, 140*sc.mil, 6*sc.inch
212+ # cond = 1e7
213+ # er
214+ #
215+ # # Load simulated data
216+ # filename = os.path.join('data', 'cavity.s2p')
217+ # dir_name = os.path.dirname(__file__)
218+ # filename = os.path.join(dir_name, filename)
219+ # data = rf.Network(filename)
220+ #
221+ # # Unpack
222+ # f = data.f
223+ # s21 = data.s[:, 1, 0]
224+ #
225+ # # Find resonances
226+ # fres_list = wg.find_resonances(f, wg.db20(s21), height=-90)
227+ #
228+ # # Get Q-factor
229+ # fres, q0, ql, _ = wg.find_qfactor(f, np.abs(s21), fres_list, fspan=5e7, ncol=6, figsize=(14,8), debug=debug)
230+ #
231+ # # Resonant frequencies (theory)
232+ # ell = np.arange(3, 3 + len(fres))
233+ # fres_theory = wg.resonant_frequency(a, b, d, l=ell)
234+ # np.testing.assert_almost_equal(fres / 1e9, fres_theory / 1e9, decimal=1)
235+ #
236+ # # Q-factor (theory)
237+ # qc_theory = wg.qfactor_conduction(a, b, d, cond, l=ell)
238+ #
239+ # # Plot Q-factor
240+ # if debug:
241+ # plt.figure()
242+ # plt.plot(ell, q0, 'bo--', label="Q-factor (corrected)")
243+ # plt.plot(ell, ql, 'ro--', label="Q-factor (loaded)")
244+ # plt.plot(ell, qc_theory, 'ko--', label='Theory')
245+ # plt.xlim(xmin=0)
246+ # plt.legend()
247+ # plt.show()
248+ #
249+ # # Get conductivity from Q-factor
250+ # cond_q = wg.q2conductivity(fres, q0, a, b, d, l=ell)
251+ # cond_theory = wg.q2conductivity(fres_theory, qc_theory, a, b, d, l=ell)
252+ #
253+ # # Plot conductivity
254+ # if debug:
255+ # plt.figure()
256+ # plt.plot(fres, cond_q, 'bo-', label="From Q-factor")
257+ # plt.plot(fres_theory, cond_theory, 'ro--', label="From theory")
258+ # plt.axhline(cond, c='k', ls='--')
259+ # plt.ylabel("Conductivity (S/m)")
260+ # plt.xlabel("Frequency (GHz)")
261+ # plt.show()
262+ #
263+ # # Test
264+ # np.testing.assert_allclose(cond_q, cond*np.ones_like(cond_q), atol=1e6)
265+ # np.testing.assert_allclose(cond_theory, cond * np.ones_like(cond_q), atol=1)
106266
107267
108268if __name__ == "__main__" :
109269
110- test_example_6p1 ()
111- test_example_6p3 ()
112- test_problem_6p9 ()
270+ # test_example_6p1()
271+ # test_example_6p3()
272+ # test_problem_6p9()
273+ # test_problem_6p23()
274+ test_simulated_cavity (debug = True )
0 commit comments