| 1 |
import os |
| 2 |
import unittest |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
import pytest |
| 6 |
from numpy.testing import assert_almost_equal, assert_array_almost_equal |
| 7 |
|
| 8 |
import skrf as rf |
| 9 |
from skrf.media import Coaxial |
| 10 |
|
| 11 |
|
| 12 |
class MediaTestCase(unittest.TestCase): |
| 13 |
""" |
| 14 |
|
| 15 |
""" |
| 16 |
def setUp(self): |
| 17 |
""" |
| 18 |
|
| 19 |
""" |
| 20 |
self.files_dir = os.path.join( |
| 21 |
os.path.dirname(os.path.abspath(__file__)), |
| 22 |
'qucs_prj' |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
def test_line(self): |
| 28 |
""" |
| 29 |
""" |
| 30 |
fname = os.path.join(self.files_dir,\ |
| 31 |
'coaxial.s2p') |
| 32 |
qucs_ntwk = rf.Network(fname) |
| 33 |
|
| 34 |
a_media = Coaxial( |
| 35 |
frequency = qucs_ntwk.frequency, |
| 36 |
Dint=1e-3, Dout=3e-3, epsilon_r=2.29, |
| 37 |
tan_delta=4e-4, sigma=1./1.68e-8, |
| 38 |
z0_port = 50. |
| 39 |
) |
| 40 |
skrf_ntwk = a_media.line(200e-3,'m') |
| 41 |
|
| 42 |
|
| 43 |
self.assertTrue( |
| 44 |
max(abs(skrf_ntwk.s_mag[:,1,0] - qucs_ntwk.s_mag[:,1,0])) < 1e-3 |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
def test_init_from_attenuation_VF_units(self): |
| 49 |
""" |
| 50 |
Test the attenuation unit conversions in the Coaxial classmethod |
| 51 |
`from_attenuation_VF_units`. |
| 52 |
""" |
| 53 |
|
| 54 |
|
| 55 |
rng = np.random.default_rng() |
| 56 |
frequency = rf.Frequency(rng.random(), unit='GHz', npoints=1) |
| 57 |
_att = rng.random() |
| 58 |
|
| 59 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=_att, unit='dB/m') |
| 60 |
assert_almost_equal(coax.gamma.real, rf.db_2_np(_att)) |
| 61 |
|
| 62 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=_att, unit='dB/100m') |
| 63 |
assert_almost_equal(coax.gamma.real, rf.db_2_np(_att)/100) |
| 64 |
|
| 65 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=_att, unit='dB/feet') |
| 66 |
assert_almost_equal(coax.gamma.real, rf.db_2_np(_att)*rf.meter_2_feet()) |
| 67 |
|
| 68 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=_att, unit='dB/100feet') |
| 69 |
assert_almost_equal(coax.gamma.real, rf.db_2_np(_att)/100*rf.meter_2_feet()) |
| 70 |
|
| 71 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=_att, unit='Np/m') |
| 72 |
assert_almost_equal(coax.gamma.real, _att) |
| 73 |
|
| 74 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=_att, unit='Np/feet') |
| 75 |
assert_almost_equal(coax.gamma.real, _att*rf.meter_2_feet()) |
| 76 |
|
| 77 |
with self.assertRaises(ValueError): |
| 78 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=np.array([.1, .2]), unit='Np/feet') |
| 79 |
frequency = rf.Frequency(1., 1.1, unit='GHz', npoints=2) |
| 80 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, VF=1, att=np.array([.1, .2]), unit='Np/feet') |
| 81 |
self.assertEqual( coax.frequency.f.shape, (2,)) |
| 82 |
|
| 83 |
|
| 84 |
def test_init_from_attenuation_VF_array_att(self): |
| 85 |
""" |
| 86 |
Test passing array as attenuation in the Coaxial classmethod |
| 87 |
`from_attenuation_VF_units`. |
| 88 |
""" |
| 89 |
|
| 90 |
|
| 91 |
frequency = rf.Frequency(start=1, stop=2, unit='GHz', npoints=101) |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
att = 0 + 0.0826*np.sqrt(frequency.f_scaled) + 0.0129*frequency.f_scaled |
| 96 |
|
| 97 |
coax = Coaxial.from_attenuation_VF(frequency=frequency, att=att, unit='dB/m') |
| 98 |
|
| 99 |
assert_array_almost_equal(rf.db_2_np(att), coax.gamma.real) |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
frequency2 = rf.Frequency(start=1, stop=2, unit='GHz', npoints=10) |
| 104 |
with self.assertRaises(ValueError): |
| 105 |
coax = Coaxial.from_attenuation_VF(frequency=frequency2, att=att) |
| 106 |
|
| 107 |
def test_R(self): |
| 108 |
freq = rf.Frequency(0, 100, 2, unit='GHz') |
| 109 |
|
| 110 |
rho = 1e-7 |
| 111 |
dint = 0.44e-3 |
| 112 |
coax = Coaxial(freq, z0_port = 50, Dint = dint, Dout = 1.0e-3, |
| 113 |
sigma = 1/rho) |
| 114 |
|
| 115 |
dc_res = rho / (np.pi * (dint/2)**2) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
with pytest.warns(RuntimeWarning, match="divide by zero"): |
| 120 |
R_simple = coax.Rs/(2*np.pi)*(1/coax.a + 1/coax.b) |
| 121 |
|
| 122 |
self.assertTrue(abs(1 - coax.R[0]/dc_res) < 1e-2) |
| 123 |
self.assertTrue(abs(1 - coax.R[1]/R_simple[1]) < 1e-2) |
| 124 |
|
| 125 |
def test_LC(self): |
| 126 |
"""Assert that LC = mu*eps_prime.""" |
| 127 |
coax = Coaxial( |
| 128 |
frequency = rf.Frequency(1, 10, npoints=100, unit='GHz'), |
| 129 |
Dint=1e-3, Dout=3e-3, epsilon_r=2.29, |
| 130 |
tan_delta=4e-4, sigma=1./1.68e-8, |
| 131 |
z0_port = 50. |
| 132 |
) |
| 133 |
assert_array_almost_equal(coax.L*coax.C, rf.mu_0*coax.epsilon_prime) |