| 1 |
import os |
| 2 |
import unittest |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
|
| 6 |
import skrf as rf |
| 7 |
from skrf.frequency import Frequency |
| 8 |
from skrf.media import MLine |
| 9 |
|
| 10 |
try: |
| 11 |
from matplotlib import pyplot as plt |
| 12 |
rf.stylely() |
| 13 |
except ImportError: |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class MLineTestCase(unittest.TestCase): |
| 19 |
""" |
| 20 |
Testcase for the MLine Media |
| 21 |
""" |
| 22 |
def setUp(self): |
| 23 |
""" |
| 24 |
Read in all the network data required for tests |
| 25 |
""" |
| 26 |
self.data_dir_qucs = os.path.dirname(os.path.abspath(__file__)) + \ |
| 27 |
'/qucs_prj/' |
| 28 |
self.data_dir_ads = os.path.dirname(os.path.abspath(__file__)) + \ |
| 29 |
'/ads/' |
| 30 |
|
| 31 |
self.ref_qucs = [ |
| 32 |
{'model': 'hammerstadjensen', 'disp': 'hammerstadjensen', 'color': 'r', |
| 33 |
'n': rf.Network(os.path.join(self.data_dir_qucs, |
| 34 |
'mline,hammerstad,hammerstad.s2p'))}, |
| 35 |
{'model': 'hammerstadjensen', 'disp': 'kirschningjansen', 'color': 'c', |
| 36 |
'n': rf.Network(os.path.join(self.data_dir_qucs, |
| 37 |
'mline,hammerstad,kirschning.s2p'))}, |
| 38 |
{'model': 'hammerstadjensen', 'disp': 'kobayashi', 'color': 'k', |
| 39 |
'n': rf.Network(os.path.join(self.data_dir_qucs, |
| 40 |
'mline,hammerstad,kobayashi.s2p'))}, |
| 41 |
{'model': 'hammerstadjensen', 'disp': 'yamashita', 'color': 'g', |
| 42 |
'n': rf.Network(os.path.join(self.data_dir_qucs, |
| 43 |
'mline,hammerstad,yamashita.s2p'))}, |
| 44 |
{'model': 'wheeler', 'disp': 'schneider', 'color': 'm', |
| 45 |
'n': rf.Network(os.path.join(self.data_dir_qucs, |
| 46 |
'mline,wheeler,schneider.s2p'))}, |
| 47 |
{'model': 'schneider', 'disp': 'schneider', 'color': 'b', |
| 48 |
'n': rf.Network(os.path.join(self.data_dir_qucs, |
| 49 |
'mline,schneider,schneider.s2p'))} |
| 50 |
] |
| 51 |
|
| 52 |
|
| 53 |
self.ref_ads = [ |
| 54 |
{'diel': 'frequencyinvariant', 'disp': 'kirschningjansen', 'color': 'r', |
| 55 |
'n': rf.Network(os.path.join(self.data_dir_ads, |
| 56 |
'mlin,freqencyinvariant,kirschning.s2p'))}, |
| 57 |
{'diel': 'djordjevicsvensson', 'disp': 'kirschningjansen', 'color': 'c', |
| 58 |
'n': rf.Network(os.path.join(self.data_dir_ads, |
| 59 |
'mlin,djordjevicsvensson,kirschning.s2p'))}, |
| 60 |
{'diel': 'frequencyinvariant', 'disp': 'kobayashi', 'color': 'k', |
| 61 |
'n': rf.Network(os.path.join(self.data_dir_ads, |
| 62 |
'mlin,freqencyinvariant,kobayashi.s2p'))}, |
| 63 |
{'diel': 'djordjevicsvensson', 'disp': 'kobayashi', 'color': 'g', |
| 64 |
'n': rf.Network(os.path.join(self.data_dir_ads, |
| 65 |
'mlin,djordjevicsvensson,kobayashi.s2p'))}, |
| 66 |
{'diel': 'frequencyinvariant', 'disp': 'yamashita', 'color': 'm', |
| 67 |
'n': rf.Network(os.path.join(self.data_dir_ads, |
| 68 |
'mlin,freqencyinvariant,yamashita.s2p'))}, |
| 69 |
{'diel': 'djordjevicsvensson', 'disp': 'yamashita', 'color': 'b', |
| 70 |
'n': rf.Network(os.path.join(self.data_dir_ads, |
| 71 |
'mlin,djordjevicsvensson,yamashita.s2p'))} |
| 72 |
] |
| 73 |
|
| 74 |
|
| 75 |
self.verbose = False |
| 76 |
self.w = 3.00e-3 |
| 77 |
self.h = 1.55e-3 |
| 78 |
self.t = 35e-6 |
| 79 |
self.l = 25e-3 |
| 80 |
self.ep_r = 4.413 |
| 81 |
self.tand = 0.0182 |
| 82 |
self.rho = 1.7e-8 |
| 83 |
self.d = 0.15e-6 |
| 84 |
self.f_et = 1e9 |
| 85 |
|
| 86 |
|
| 87 |
def test_z0_ep_reff(self): |
| 88 |
""" |
| 89 |
Test against characterisitc impedance from another calculator using |
| 90 |
Hammerstadt-Jensen model |
| 91 |
http://web.mit.edu/~geda/arch/i386_rhel3/versions/20050830/html/mcalc-1.5/ |
| 92 |
""" |
| 93 |
freq = Frequency(1, 1, 1, 'GHz') |
| 94 |
mline1 = MLine(frequency = freq, z0_port = 50., |
| 95 |
w = self.w, h = self.h, t = self.t, |
| 96 |
ep_r = self.ep_r, rho = self.rho, |
| 97 |
tand = self.tand, rough = self.d, |
| 98 |
diel = 'frequencyinvariant', disp = 'hammerstadjensen', |
| 99 |
compatibility_mode = 'qucs') |
| 100 |
|
| 101 |
|
| 102 |
mline2 = MLine(frequency = freq, z0_port = 50., |
| 103 |
w = self.w, h = self.h, |
| 104 |
ep_r = self.ep_r, rho = self.rho, |
| 105 |
tand = self.tand, rough = self.d, |
| 106 |
diel = 'frequencyinvariant', disp = 'hammerstadjensen', |
| 107 |
compatibility_mode = 'qucs') |
| 108 |
|
| 109 |
|
| 110 |
mline3 = MLine(frequency = freq, z0_port = 50., |
| 111 |
w = self.w, h = self.h, t = 0, |
| 112 |
ep_r = self.ep_r, rho = self.rho, |
| 113 |
tand = self.tand, rough = self.d, |
| 114 |
diel = 'frequencyinvariant', disp = 'hammerstadjensen', |
| 115 |
compatibility_mode = 'qucs') |
| 116 |
|
| 117 |
self.assertTrue(np.abs((mline1.z0[0] - 49.142) / 49.142) < 0.01) |
| 118 |
self.assertTrue(np.abs((mline1.ep_reff_f[0] - 3.324) / 3.324) < 0.01) |
| 119 |
self.assertTrue(np.abs(mline2.w_eff - mline2.w) < 1e-16) |
| 120 |
self.assertTrue(np.abs(mline2.alpha_conductor) < 1e-16) |
| 121 |
self.assertTrue(np.abs(mline3.w_eff - mline3.w) < 1e-16) |
| 122 |
self.assertTrue(np.abs(mline3.alpha_conductor) < 1e-16) |
| 123 |
|
| 124 |
|
| 125 |
def test_line_qucs(self): |
| 126 |
""" |
| 127 |
Test against the Qucs project results |
| 128 |
""" |
| 129 |
if self.verbose: |
| 130 |
fig, axs = plt.subplots(2, 2, figsize = (8,6)) |
| 131 |
fig.suptitle('qucs/skrf') |
| 132 |
fig2, axs2 = plt.subplots(2, 2, figsize = (8,6)) |
| 133 |
fig2.suptitle('ads/skrf residuals') |
| 134 |
|
| 135 |
limit_db = 0.1 |
| 136 |
limit_deg = 1. |
| 137 |
|
| 138 |
for ref in self.ref_qucs: |
| 139 |
mline = MLine(frequency = ref['n'].frequency, z0_port = 50., |
| 140 |
w = self.w, h = self.h, t = self.t, |
| 141 |
ep_r = self.ep_r, rho = self.rho, |
| 142 |
tand = self.tand, rough = self.d, |
| 143 |
model = ref['model'], disp = ref['disp'], |
| 144 |
diel = 'frequencyinvariant', |
| 145 |
compatibility_mode = 'qucs') |
| 146 |
line = mline.line(d=self.l, unit='m') |
| 147 |
line.name = 'skrf,qucs' |
| 148 |
|
| 149 |
|
| 150 |
res = line / ref['n'] |
| 151 |
res.name = 'residuals ' + ref['n'].name |
| 152 |
|
| 153 |
|
| 154 |
self.assertTrue(np.all(np.abs(res.s_db) < limit_db)) |
| 155 |
self.assertTrue(np.all(np.abs(res.s_deg) < limit_deg)) |
| 156 |
|
| 157 |
if self.verbose: |
| 158 |
line.plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'], |
| 159 |
linestyle = 'none', marker = 'x') |
| 160 |
ref['n'].plot_s_db(0, 0, ax = axs[0, 0], color = ref['color']) |
| 161 |
res.plot_s_db(0, 0, ax = axs2[0, 0], linestyle = 'dashed', |
| 162 |
color = ref['color']) |
| 163 |
|
| 164 |
line.plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'], |
| 165 |
linestyle = 'none', marker = 'x') |
| 166 |
ref['n'].plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color']) |
| 167 |
res.plot_s_deg(0, 0, ax = axs2[0, 1], linestyle = 'dashed', |
| 168 |
color = ref['color']) |
| 169 |
|
| 170 |
line.plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'], |
| 171 |
linestyle = 'none', marker = 'x') |
| 172 |
ref['n'].plot_s_db(1, 0, ax = axs[1, 0], color = ref['color']) |
| 173 |
res.plot_s_db(1, 0, ax = axs2[1, 0], linestyle = 'dashed', |
| 174 |
color = ref['color']) |
| 175 |
|
| 176 |
line.plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'], |
| 177 |
linestyle = 'none', marker = 'x') |
| 178 |
ref['n'].plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color']) |
| 179 |
res.plot_s_deg(1, 0, ax = axs2[1, 1], linestyle = 'dashed', |
| 180 |
color = ref['color']) |
| 181 |
|
| 182 |
|
| 183 |
if self.verbose: |
| 184 |
axs[1, 0].legend(prop={'size': 6}) |
| 185 |
axs[0, 0].get_legend().remove() |
| 186 |
axs[0, 1].get_legend().remove() |
| 187 |
axs[1, 1].get_legend().remove() |
| 188 |
fig.tight_layout() |
| 189 |
|
| 190 |
axs2[1, 0].legend(prop={'size': 6}) |
| 191 |
axs2[0, 0].get_legend().remove() |
| 192 |
axs2[0, 1].get_legend().remove() |
| 193 |
axs2[1, 1].get_legend().remove() |
| 194 |
fig2.tight_layout() |
| 195 |
|
| 196 |
def test_line_ads(self): |
| 197 |
""" |
| 198 |
Test against the ADS results |
| 199 |
""" |
| 200 |
if self.verbose: |
| 201 |
fig, axs = plt.subplots(2, 2, figsize = (8,6)) |
| 202 |
fig.suptitle('ads/skrf') |
| 203 |
fig2, axs2 = plt.subplots(2, 2, figsize = (8,6)) |
| 204 |
fig2.suptitle('ads/skrf residuals') |
| 205 |
|
| 206 |
|
| 207 |
limit_db = 0.1 |
| 208 |
limit_deg = 1. |
| 209 |
|
| 210 |
for ref in self.ref_ads: |
| 211 |
mline = MLine(frequency = ref['n'].frequency, z0_port = 50., |
| 212 |
w = self.w, h = self.h, t = self.t, |
| 213 |
ep_r = self.ep_r, rho = self.rho, |
| 214 |
tand = self.tand, rough = self.d, |
| 215 |
model = 'hammerstadjensen', disp = ref['disp'], |
| 216 |
diel = ref['diel']) |
| 217 |
line = mline.line(d=self.l, unit='m') |
| 218 |
line.name = 'skrf,ads' |
| 219 |
|
| 220 |
|
| 221 |
res = line / ref['n'] |
| 222 |
res.name = 'residuals ' + ref['n'].name |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
self.assertTrue( |
| 228 |
np.all(np.abs(res.s_db[:, 0, 0]) < 10. *limit_db)) |
| 229 |
self.assertTrue( |
| 230 |
np.all(np.abs(res.s_deg[:, 0, 0]) < 10. * limit_deg)) |
| 231 |
self.assertTrue(np.all(np.abs(res.s_db[:, 1, 0]) < limit_db)) |
| 232 |
self.assertTrue(np.all(np.abs(res.s_deg[:, 1, 0]) < limit_deg)) |
| 233 |
|
| 234 |
if self.verbose: |
| 235 |
line.plot_s_db(0, 0, ax = axs[0, 0], color = ref['color'], |
| 236 |
linestyle = 'none', marker = 'x') |
| 237 |
ref['n'].plot_s_db(0, 0, ax = axs[0, 0], color = ref['color']) |
| 238 |
res.plot_s_db(0, 0, ax = axs2[0, 0], linestyle = 'dashed', |
| 239 |
color = ref['color']) |
| 240 |
|
| 241 |
line.plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color'], |
| 242 |
linestyle = 'none', marker = 'x') |
| 243 |
ref['n'].plot_s_deg(0, 0, ax = axs[0, 1], color = ref['color']) |
| 244 |
res.plot_s_deg(0, 0, ax = axs2[0, 1], linestyle = 'dashed', |
| 245 |
color = ref['color']) |
| 246 |
|
| 247 |
line.plot_s_db(1, 0, ax = axs[1, 0], color = ref['color'], |
| 248 |
linestyle = 'none', marker = 'x') |
| 249 |
ref['n'].plot_s_db(1, 0, ax = axs[1, 0], color = ref['color']) |
| 250 |
res.plot_s_db(1, 0, ax = axs2[1, 0], linestyle = 'dashed', |
| 251 |
color = ref['color']) |
| 252 |
|
| 253 |
line.plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color'], |
| 254 |
linestyle = 'none', marker = 'x') |
| 255 |
ref['n'].plot_s_deg(1, 0, ax = axs[1, 1], color = ref['color']) |
| 256 |
res.plot_s_deg(1, 0, ax = axs2[1, 1], linestyle = 'dashed', |
| 257 |
color = ref['color']) |
| 258 |
|
| 259 |
|
| 260 |
if self.verbose: |
| 261 |
axs[1, 0].legend(prop={'size': 6}) |
| 262 |
axs[0, 0].get_legend().remove() |
| 263 |
axs[0, 1].get_legend().remove() |
| 264 |
axs[1, 1].get_legend().remove() |
| 265 |
fig.tight_layout() |
| 266 |
|
| 267 |
axs2[1, 0].legend(prop={'size': 6}) |
| 268 |
axs2[0, 0].get_legend().remove() |
| 269 |
axs2[0, 1].get_legend().remove() |
| 270 |
axs2[1, 1].get_legend().remove() |
| 271 |
fig2.tight_layout() |
| 272 |
|
| 273 |
def test_alpha_warning(self): |
| 274 |
""" |
| 275 |
Test if warns when t < 3 * skin_depth |
| 276 |
""" |
| 277 |
freq = Frequency(1, 1, 1, 'MHz') |
| 278 |
with self.assertWarns(RuntimeWarning) as context: |
| 279 |
mline = MLine(frequency = freq, z0_port = 50., |
| 280 |
w = self.w, h = self.h, t = self.t, |
| 281 |
ep_r = self.ep_r, rho = self.rho, |
| 282 |
tand = self.tand, rough = self.d, |
| 283 |
diel = 'frequencyinvariant', disp = 'hammerstadjensen') |