Debian Codegraph @

skrf/tests/test_circuit.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
9
10 class CircuitTestConstructor(unittest.TestCase):
11 """
12 Various tests on the Circuit constructor.
13 """
14 def setUp(self):
15 # Importing network examples
16 self.test_dir = os.path.dirname(os.path.abspath(__file__))+'/'
17 self.ntwk1 = rf.Network(os.path.join(self.test_dir, 'ntwk1.s2p'))
18 self.ntwk2 = rf.Network(os.path.join(self.test_dir, 'ntwk2.s2p'))
19 self.freq = self.ntwk1.frequency
20 # circuit external ports
21 self.port1 = rf.Circuit.Port(self.freq, name='Port1')
22 self.port2 = rf.Circuit.Port(self.freq, name='Port2')
23 # circuit connections
24 self.connections = [[(self.port1, 0), (self.ntwk1, 0)],
25 [(self.ntwk1, 1), (self.ntwk2, 0)],
26 [(self.ntwk2, 1), (self.port2, 0)]]
27 self.circuit = rf.Circuit(self.connections)
28
29 def test_all_networks_have_name(self):
30 """
31 Check that a Network without name raises an exception
32 """
33 _ntwk1 = self.ntwk1.copy()
34 connections = [[(self.port1, 0), (_ntwk1, 0)],
35 [(_ntwk1, 1), (self.ntwk2, 0)],
36 [(self.ntwk2, 1), (self.port2, 0)]]
37
38 _ntwk1.name = []
39 self.assertRaises(AttributeError, rf.Circuit, connections)
40
41 _ntwk1.name = ''
42 self.assertRaises(AttributeError, rf.Circuit, connections)
43
44 def test_all_networks_have_same_frequency(self):
45 """
46 Check that a Network with a different frequency than the other
47 raises an exception
48 """
49 _ntwk1 = self.ntwk1.copy()
50 connections = [[(self.port1, 0), (_ntwk1, 0)],
51 [(_ntwk1, 1), (self.ntwk2, 0)],
52 [(self.ntwk2, 1), (self.port2, 0)]]
53
54 _ntwk1.frequency = rf.Frequency(start=1, stop=1, npoints=1, unit='GHz')
55 self.assertRaises(AttributeError, rf.Circuit, connections)
56
57 def test_no_duplicate_node(self):
58 """
59 Check that a circuit description has no duplicated (network, port)
60 """
61 # (port1, 0) is found twice in the connections description
62 connections = [[(self.port1, 0), (self.ntwk1, 0)],
63 [(self.ntwk1, 1), (self.ntwk2, 0)],
64 [(self.ntwk2, 1), (self.port1, 0)]]
65 self.assertRaises(AttributeError, rf.Circuit, connections)
66
67 def test_s_active(self):
68 """
69 Test the active s-parameter of a 2-ports network
70 """
71 # s_act should be equal to s11 if a = [1,0]
72 assert_array_almost_equal(self.circuit.s_active([1, 0])[:,0], self.circuit.s_external[:,0,0])
73 # s_act should be equal to s22 if a = [0,1]
74 assert_array_almost_equal(self.circuit.s_active([0, 1])[:,1], self.circuit.s_external[:,1,1])
75
76 def test_auto_reduce(self):
77 """
78 Test the auto_reduce parameter of the Circuit constructor
79 """
80 # Explicitly enable the `reduce_circuit` method
81 reduced_circuit = rf.Circuit(self.connections, auto_reduce=True)
82 assert_array_almost_equal(self.circuit.s_external, reduced_circuit.s_external)
83 self.assertNotEqual(self.circuit.dim, reduced_circuit.dim)
84
85 # Implicitly enable the `reduce_circuit` method
86 implicit_reduced_circuit = rf.Circuit(self.connections, max_nports=10)
87 self.assertEqual(reduced_circuit.dim, implicit_reduced_circuit.dim)
88
89 def test_auto_reduce_with_passed_arguments(self):
90 """
91 Test the auto_reduce parameter of the Circuit constructor with passed arguments
92 """
93 # Test with max_nports=1 and dynamic_networks in a tuple or a list
94 kwargs = [('max_nports', 1),
95 ('dynamic_networks', (self.ntwk2,)),
96 ('dynamic_networks', [self.ntwk2])]
97
98 # No connections should be reduced
99 for key, value in kwargs:
100 circuit = rf.Circuit(self.connections, **{key: value})
101 assert_array_almost_equal(self.circuit.s_external, circuit.s_external)
102 self.assertEqual(circuit.connections, self.connections)
103
104 def test_update_networks(self):
105 """
106 Test the update_networks method of the Circuit class
107 """
108 # Create an abnormal circuit with a random network
109 ntwk3 = self.ntwk1.copy()
110 ntwk3.name = 'ntwk3'
111 connections = [[(self.port1, 0), (self.ntwk1, 0)],
112 [(self.ntwk1, 1), (ntwk3, 0)],
113 [(ntwk3, 1), (self.port2, 0)]]
114 circuit = rf.Circuit(connections, dynamic_networks=(ntwk3,))
115
116 # should raise an exception if passing a network not in the circuit
117 with self.assertRaises(ValueError):
118 _ = circuit.update_networks(networks=(self.ntwk2,))
119
120 # Update the circuit with the self.ntwk2
121 ntwk3.s = self.ntwk2.s
122
123 # Check the result type and values
124 circuit_updated = circuit.update_networks(networks=(ntwk3,))
125 self.assertTrue(isinstance(circuit_updated, rf.Circuit))
126 assert_array_almost_equal(self.circuit.s_external, circuit_updated.s_external)
127
128 # Check the result type and values when updating the circuit inplace
129 none_result = circuit.update_networks(networks=(ntwk3,), inplace=True)
130 self.assertIsNone(none_result)
131 assert_array_almost_equal(self.circuit.s_external, circuit.s_external)
132
133 def test_cache_attributes(self):
134 """
135 Test the cached attributes of the Circuit
136 """
137 init_circuit = rf.Circuit(self.connections)
138
139 cached_attributes = rf.Circuit.CACHEDPROPERTIES
140
141 # Initial circuit should not have cached attributes
142 for attr in cached_attributes:
143 self.assertFalse(init_circuit.__dict__.get(attr, False))
144
145 # Access the attributes to cache them
146 values = [getattr(init_circuit, attr) for attr in cached_attributes]
147
148 # Check that the cached attributes are set correctly
149 for attr, value in zip(cached_attributes, values):
150 assert_array_almost_equal(init_circuit.__dict__.get(attr, 0.0), value)
151
152 # Modify connections to invalidate the cache
153 init_circuit.connections = self.connections
154 for attr in cached_attributes:
155 self.assertFalse(init_circuit.__dict__.get(attr, False))
156
157 def test_auxiliary_matrix(self):
158 """
159 Test the auxiliary matrix [T] of the Circuit
160 """
161 # Calculate the auxiliary matrix T and C @ X
162 X, C = self.circuit.X, self.circuit.C
163 X_F, C_F = self.circuit.X_F, self.circuit.C_F
164 rst, T = - C @ X, self.circuit.T
165
166 # Check that auxiliary_matrices is equal to initial values
167 assert_array_almost_equal(T, rst)
168 assert_array_almost_equal(X, X_F)
169 assert_array_almost_equal(C, C_F)
170
171 # Check the memory layout of the matrices
172 self.assertTrue(rst.flags['C_CONTIGUOUS'])
173 self.assertTrue(T.flags['F_CONTIGUOUS'])
174 self.assertTrue(self.circuit.C.flags['C_CONTIGUOUS'])
175 self.assertTrue(self.circuit.C_F.flags['F_CONTIGUOUS'])
176 self.assertTrue(self.circuit.X.flags['C_CONTIGUOUS'])
177 self.assertTrue(self.circuit.X_F.flags['F_CONTIGUOUS'])
178
179 class CircuitClassMethods(unittest.TestCase):
180 """
181 Test the various class methods of Circuit such as Ground, Port, etc.
182 """
183 def setUp(self):
184 self.freq = rf.Frequency(start=1, stop=2, npoints=101, unit='GHz')
185 self.media = rf.DefinedGammaZ0(self.freq)
186
187 def test_ground(self):
188 """
189 Ground object are infinite shunt admittance (ie. a 2-port short)
190 """
191 # should raise an exception if no name is passed
192 with self.assertRaises(TypeError):
193 gnd = rf.Circuit.Ground(self.freq)
194
195 gnd = rf.Circuit.Ground(self.freq, 'gnd')
196 gnd_ref = rf.Network(frequency=self.freq,
197 s=np.tile(np.array([[-1,]]),
198 (len(self.freq),1,1)))
199
200 assert_array_almost_equal(gnd.s, gnd_ref.s)
201
202
203 def test_open(self):
204 """
205 Open object are infinite series resistance (ie. a 2-port open)
206 """
207 # should raise an exception if no name is passed
208 with self.assertRaises(TypeError):
209 opn = rf.Circuit.Open(self.freq)
210
211 opn = rf.Circuit.Open(self.freq, 'open')
212 opn_ref = rf.Network(frequency=self.freq,
213 s=np.tile(np.array([[1]]),
214 (len(self.freq),1,1)))
215
216 assert_array_almost_equal(opn.s, opn_ref.s)
217
218 def test_series_impedance(self):
219 z0s = [1, 50]
220 Zs = [1, 1 + 1j, rf.INF]
221 for z0 in z0s:
222 for Z in Zs:
223 assert_array_almost_equal(
224 rf.Circuit.SeriesImpedance(self.freq, Z, 'imp', z0=z0).s,
225 self.media.resistor(Z, z0=z0).s
226 )
227
228 # Z=0 is a thru
229 assert_array_almost_equal(
230 rf.Circuit.SeriesImpedance(self.freq, Z=0, name='imp').s,
231 self.media.thru().s
232 )
233
234 def test_shunt_admittance(self):
235 z0s = [1, 50]
236 Ys = [1, 1 + 1j, rf.INF]
237 for z0 in z0s:
238 for Y in Ys:
239 assert_array_almost_equal(
240 rf.Circuit.ShuntAdmittance(self.freq, Y, 'imp', z0=z0).s,
241 self.media.shunt(self.media.load(rf.zl_2_Gamma0(z0, 1/Y))).s
242 )
243
244 # Y=INF is a a 2-ports short
245 assert_array_almost_equal(
246 rf.Circuit.ShuntAdmittance(self.freq, rf.INF, 'imp').s,
247 self.media.short(nports=2).s
248 )
249
250 class CircuitTestWilkinson(unittest.TestCase):
251 """
252 Create a Wilkinson power divider Circuit [#]_ and test the results
253 against network.connect.
254 The results in [#]_ do not agree due to an error in the formula (3)
255 for mismatched intersections.
256
257 References
258 ----------
259 .. [#] https://en.wikipedia.org/wiki/Wilkinson_power_divider
260 .. [#] P. Hallbjörner, Microw. Opt. Technol. Lett. 38, 99 (2003).
261
262
263 """
264 def setUp(self):
265 """
266 Circuit setup
267 """
268 self.test_dir = os.path.dirname(os.path.abspath(__file__))+'/'
269 self.freq = rf.Frequency(start=1, stop=2, npoints=101, unit='GHz')
270 # characteristic impedance of the ports
271 Z0_ports = 50
272
273 # resistor
274 self.R = 100
275 self.line_resistor = rf.media.DefinedGammaZ0(frequency=self.freq, z0=Z0_ports)
276 self.resistor = self.line_resistor.resistor(self.R, name='resistor')
277
278 # branches
279 Z0_branches = np.sqrt(2)*Z0_ports
280 self.line_branches = rf.media.DefinedGammaZ0(frequency=self.freq, z0=Z0_branches)
281 self.branch1 = self.line_branches.line(90, unit='deg', name='branch1')
282 self.branch2 = self.line_branches.line(90, unit='deg', name='branch2')
283
284 # ports
285 port1 = rf.Circuit.Port(self.freq, name='port1', z0=50)
286 port2 = rf.Circuit.Port(self.freq, name='port2', z0=50)
287 port3 = rf.Circuit.Port(self.freq, name='port3', z0=50)
288
289 # Connection setup
290 self.connections = [
291 [(port1, 0), (self.branch1, 0), (self.branch2, 0)],
292 [(port2, 0), (self.branch1, 1), (self.resistor, 0)],
293 [(port3, 0), (self.branch2, 1), (self.resistor, 1)]
294 ]
295
296 self.C = rf.Circuit(self.connections)
297
298 # theoretical results from ref P.Hallbjörner (2003)
299 self.X1_nn = np.array([1 - np.sqrt(2), -1, -1])/(1 + np.sqrt(2))
300 self.X2_nn = np.array([1 - np.sqrt(2), -3 + np.sqrt(2),
301 -1 - np.sqrt(2)])/(3 + np.sqrt(2))
302
303 self.X1_m1 = 2 / (1 + np.sqrt(2))
304 self.X1_m2 = np.sqrt(2) / (1 + np.sqrt(2))
305 self.X1_m3 = np.sqrt(2) / (1 + np.sqrt(2))
306
307 self.X2_m1 = 4 / (3 + np.sqrt(2))
308 self.X2_m2 = 2*np.sqrt(2) / (3 + np.sqrt(2))
309 self.X2_m3 = 2 / (3 + np.sqrt(2))
310
311 self.X1 = np.array([[0, self.X1_m2, self.X1_m3],
312 [self.X1_m1, 0, self.X1_m3],
313 [self.X1_m1, self.X1_m2, 0]]) + np.diag(self.X1_nn)
314 self.X2 = np.array([[0, self.X2_m2, self.X2_m3],
315 [self.X2_m1, 0, self.X2_m3],
316 [self.X2_m1, self.X2_m2, 0]]) + np.diag(self.X2_nn)
317
318
319 @unittest.expectedFailure
320 def test_sparam_individual_intersection_matrices(self):
321 """
322 Testing the individual intersection scattering matrices X_k
323 The results in [#]_ do not agree due to an error in the formula (3)
324 for mismatched intersections.
325
326 References
327 ----------
328 .. [#] P. Hallbjörner, Microw. Opt. Technol. Lett. 38, 99 (2003).
329 """
330 np.testing.assert_array_almost_equal(self.C._Xk(self.connections[0])[0], self.X1)
331 np.testing.assert_array_almost_equal(self.C._Xk(self.connections[1])[0], self.X2)
332 np.testing.assert_array_almost_equal(self.C._Xk(self.connections[2])[0], self.X2)
333
334 @unittest.expectedFailure
335 def test_sparam_global_intersection_matrix(self):
336 """
337 Testing the global intersection scattering matrix
338 The results in [#]_ do not agree due to an error in the formula (3)
339 for mismatched intersections.
340
341 References
342 ----------
343 .. [#] P. Hallbjörner, Microw. Opt. Technol. Lett. 38, 99 (2003).
344 """
345 from scipy.linalg import block_diag
346 assert_array_almost_equal(self.C.X[0], block_diag(self.X1, self.X2, self.X2) )
347
348 def test_sparam_circuit(self):
349 """
350 Testing the external scattering matrix
351 """
352 S_theoretical = np.array([[0, 1, 1],
353 [1, 0, 0],
354 [1, 0, 0]]) * (-1j/np.sqrt(2))
355
356 # extracting the external ports
357 S_ext = self.C.s_external
358
359 assert_array_almost_equal(S_ext[0], S_theoretical)
360
361 def test_compare_with_skrf_wilkison(self):
362 """
363 Create a Wilkinson power divider using skrf usual Network methods.
364 """
365 z0_port = 50
366 # require to create the three tees
367 T0 = self.line_branches.splitter(3, z0=z0_port)
368 T1 = self.line_branches.splitter(3, z0=z0_port)
369 T2 = self.line_branches.splitter(3, z0=z0_port)
370
371 _wilkinson1 = rf.connect(T0, 1, self.branch1, 0)
372 _wilkinson2 = rf.connect(_wilkinson1, 2, self.branch2, 0)
373 _wilkinson3 = rf.connect(_wilkinson2, 1, T1, 0)
374 _wilkinson4 = rf.connect(_wilkinson3, 1, T2, 0)
375 _wilkinson5 = rf.connect(_wilkinson4, 1, self.resistor, 0)
376 wilkinson = rf.innerconnect(_wilkinson5, 1, 4)
377
378 ntw_C = self.C.network
379
380 assert_array_almost_equal(ntw_C.s, wilkinson.s)
381 assert_array_almost_equal(ntw_C.z0, wilkinson.z0)
382
383 def test_compare_with_designer_wilkinson(self):
384 """
385 Compare the result with ANSYS Designer model
386
387 Built as in https://www.microwaves101.com/encyclopedias/wilkinson-power-splitters
388 """
389 designer_wilkinson = rf.Network(os.path.join(self.test_dir, 'designer_wilkinson_splitter.s3p'))
390 ntw_C = self.C.network
391
392 assert_array_almost_equal(ntw_C.s[0], designer_wilkinson.s[0], decimal=4)
393
394 def test_s_active(self):
395 """
396 Test the active s-parameter of a 3-ports network
397 """
398 # s_act should be equal to s11 if a = [1,0,0]
399 assert_array_almost_equal(self.C.network.s_active([1, 0, 0])[:,0], self.C.s_external[:,0,0])
400 # s_act should be equal to s22 if a = [0,1,0]
401 assert_array_almost_equal(self.C.network.s_active([0, 1, 0])[:,1], self.C.s_external[:,1,1])
402 # s_act should be equal to s33 if a = [0,0,1]
403 assert_array_almost_equal(self.C.network.s_active([0, 0, 1])[:,2], self.C.s_external[:,2,2])
404
405 def test_circuit_reduce_with_split_multi(self):
406 """
407 Test the reduce_circuit method with split_multi=True.
408 """
409 ntw_C = self.C.network
410
411 C_reduced = rf.Circuit(self.connections, split_multi=True)
412 ntw_C_reduced = C_reduced.network
413
414 self.assertNotEqual(self.C.dim, C_reduced.dim)
415 assert_array_almost_equal(ntw_C.s, ntw_C_reduced.s)
416
417 class CircuitTestCascadeNetworks(unittest.TestCase):
418 """
419 Build a circuit made of two Networks cascaded and compare the result
420 to usual cascading of two networks.
421 """
422 def setUp(self):
423 # Importing network examples
424 self.test_dir = os.path.dirname(os.path.abspath(__file__))+'/'
425 self.ntwk1 = rf.Network(os.path.join(self.test_dir, 'ntwk1.s2p'))
426 self.ntwk2 = rf.Network(os.path.join(self.test_dir, 'ntwk2.s2p'))
427 # ntwk3 is the cascade of ntwk1 and ntwk2, ie. self.ntwk1 ** self.ntwk2
428 self.ntwk3 = rf.Network(os.path.join(self.test_dir, 'ntwk3.s2p'))
429 self.freq = self.ntwk1.frequency
430 # circuit external ports
431 self.port1 = rf.Circuit.Port(self.freq, name='Port1')
432 self.port2 = rf.Circuit.Port(self.freq, name='Port2')
433 self.open = rf.Circuit.Open(self.freq, name='PortO')
434 _media = rf.media.DefinedGammaZ0(frequency=self.freq)
435 self.match = _media.match(name='match')
436 self.tee = _media.tee(name='tee')
437 self.thru = _media.thru()
438
439 def test_cascade(self):
440 """
441 Compare ntwk3 to the Circuit of ntwk1 and ntwk2.
442 """
443 connections = [ [(self.port1, 0), (self.ntwk1, 0)],
444 [(self.ntwk1, 1), (self.ntwk2, 0)],
445 [(self.ntwk2, 1), (self.port2, 0)] ]
446 circuit = rf.Circuit(connections)
447
448 assert_array_almost_equal(circuit.s_external, self.ntwk3.s)
449
450 def test_cascade2(self):
451 """
452 Same thing with different ordering of the connections.
453 Demonstrate that changing the connections setup order does not change
454 the result.
455 """
456 connections = [ [(self.port1, 0), (self.ntwk1, 0)],
457 [(self.ntwk2, 0), (self.ntwk1, 1)],
458 [(self.port2, 0), (self.ntwk2, 1)] ]
459 circuit = rf.Circuit(connections)
460
461 assert_array_almost_equal(circuit.s_external, self.ntwk3.s)
462
463 def test_cascade3(self):
464 """
465 Inverting the cascading network order
466 Demonstrate that changing the connections setup order does not change
467 the result (at the requirement that port impedance are the same).
468 """
469 connections = [ [(self.port1, 0), (self.ntwk2, 0)],
470 [(self.ntwk2, 1), (self.ntwk1, 0)],
471 [(self.port2, 0), (self.ntwk1, 1)] ]
472 circuit = rf.Circuit(connections)
473 ntw = self.ntwk2 ** self.ntwk1
474 assert_array_almost_equal(circuit.s_external, ntw.s)
475
476 def test_numerical_singular_corner_case(self):
477 """
478 Test a numerical singular corner case which 2 Tee Networks are cascaded.
479 Schematic of the test circuit:
480 +------+ +------+ +------------+ +------------+
481 -|0 | | 1|- p1-|T1(0) T2(1)|-p3 p1-|T1(0) T2(1)|-p2
482 | 2|----|0 | => | | => +--|T1(1) T2(2)|--+
483 -|1 | | 2|- p2-|T1(1) T2(2)|-p4 | +------------+ |
484 +------+ +------+ +------------+ +------------------+
485 T1 T2 Temp Thru
486 """
487 T1, T2 = self.tee, self.tee
488 temp = rf.connect(T1, 2, T2, 0)
489
490 # Check the s-parameters of the temporary Network
491 assert_array_almost_equal(temp.s, np.array([ [ [-0.5, 0.5, 0.5, 0.5],
492 [ 0.5, -0.5, 0.5, 0.5],
493 [ 0.5, 0.5, -0.5, 0.5],
494 [ 0.5, 0.5, 0.5, -0.5], ]
495 for _ in range(self.tee.frequency.npoints) ]
496 ,dtype=complex))
497
498 connections = [ [(self.port1, 0), (temp, 0)],
499 [(temp, 1), (temp, 3)],
500 [(self.port2, 0), (temp, 2)] ]
501
502 # Check the s-parameters of the Circuit, the Network should be equal to the thru Network
503 # RuntimeWarning not triggered after the PR #1227
504 circuit = rf.Circuit(connections)
505
506 assert_array_almost_equal(circuit.network.s, self.thru.s)
507
508 def test_open_condensation(self):
509 """
510 Test the impact of omitting an open termination in the circuit connections.
511 Compare the result of a conventional circuit setup with an open termination
512 to a condensed setup that assumes an open termination by default.
513 """
514 # Conventional setup including open termination
515 cnx_con = [ [(self.port1, 0), (self.ntwk1, 0)],
516 [(self.ntwk1, 1), (self.ntwk2, 0), (self.port2, 0)],
517 [(self.ntwk2, 1), (self.open, 0)] ]
518 ckt_con = rf.Circuit(cnx_con)
519
520 # Condensed setup where open termination is implied
521 cnx_cds = [ [(self.port1, 0), (self.ntwk1, 0)],
522 [(self.ntwk1, 1), (self.ntwk2, 0), (self.port2, 0)],
523 [(self.ntwk2, 1)] # Open termination is implied
524 ]
525
526 ckt_cds = rf.Circuit(cnx_cds)
527
528 assert_array_almost_equal(ckt_con.s_external, ckt_cds.s_external)
529
530 def test_match_condensation(self):
531 """
532 Test the impact of omitting a matching network in the circuit connections.
533 Compare the result of a conventional circuit setup with a matching network
534 to a condensed setup that assumes the matching network by default.
535 """
536 # Conventional setup including matching network
537 cnx_con = [ [(self.port1, 0), (self.ntwk1, 0)],
538 [(self.ntwk1, 1), (self.ntwk2, 0), (self.port2, 0)],
539 [(self.ntwk2, 1), (self.match, 0)] ]
540 ckt_con = rf.Circuit(cnx_con)
541
542 # Condensed setup where matching network is implied
543 cnx_cds = [ [(self.port1, 0), (self.ntwk1, 0)],
544 [(self.ntwk1, 1), (self.ntwk2, 0), (self.port2, 0)]
545 ]
546
547 ckt_cds = rf.Circuit(cnx_cds)
548
549 assert_array_almost_equal(ckt_con.s_external, ckt_cds.s_external)
550
551 class CircuitTestMultiPortCascadeNetworks(unittest.TestCase):
552 """
553 Various 1-ports, 2-ports and 4-ports circuits and associated tests
554 """
555 def test_1port_matched_load(self):
556 """
557 Connect a matched load directly to the port
558 """
559 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
560 port1 = rf.Circuit.Port(freq, name='port1')
561 line = rf.media.DefinedGammaZ0(frequency=freq)
562 match_load = line.match(name='match_load')
563
564 cnx = [
565 [(port1, 0), (match_load, 0)]
566 ]
567 cir = rf.Circuit(cnx)
568
569 assert_array_almost_equal(match_load.s, cir.s_external)
570
571 def test_1port_short(self):
572 """
573 Connect a short directly to the port
574 """
575 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
576 port1 = rf.Circuit.Port(freq, name='port1')
577 line = rf.media.DefinedGammaZ0(frequency=freq)
578 short = line.short(name='short')
579 gnd1 = rf.Circuit.Ground(freq, name='gnd')
580 # method 1 : use the Ground Network (which 2 port actually)
581 cnx = [
582 [(port1, 0), (gnd1, 0)]
583 ]
584 cir = rf.Circuit(cnx)
585 assert_array_almost_equal(short.s, cir.s_external)
586 # method 2 : use a short Network (1 port)
587 cnx = [
588 [(port1, 0), (short, 0)]
589 ]
590 cir = rf.Circuit(cnx)
591
592 assert_array_almost_equal(short.s, cir.s_external)
593
594 def test_1port_random_load(self):
595 """
596 Connect a random load directly to the port
597 """
598 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
599 port1 = rf.Circuit.Port(freq, name='port1')
600 line = rf.media.DefinedGammaZ0(frequency=freq)
601 gamma = np.random.default_rng().random((1,1)) + 1j*np.random.default_rng().random((1,1))
602 load = line.load(gamma, name='load')
603
604 cnx = [
605 [(port1, 0), (load, 0)]
606 ]
607 cir = rf.Circuit(cnx)
608
609 assert_array_almost_equal(load.s, cir.s_external)
610
611 def test_1port_matched_network_default_impedance(self):
612 """
613 Connect a random 2 port network connected to a matched load
614 """
615 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
616 a = rf.Network(name='a')
617 a.frequency = freq
618 a.s = np.random.default_rng().random(4).reshape(2, 2)
619 line = rf.media.DefinedGammaZ0(frequency=freq)
620 match_load = line.match(name='match_load')
621
622 # classic connecting
623 b = a ** match_load
624
625 # Circuit connecting
626 port1 = rf.Circuit.Port(freq, name='port1')
627 connections = [[(port1, 0), (a, 0)], [(a, 1), (match_load, 0)]]
628 circuit = rf.Circuit(connections)
629
630 assert_array_almost_equal(b.s, circuit.s_external)
631
632 def test_1port_matched_network_complex_impedance(self):
633 """
634 Connect a 2 port network to a complex impedance.
635 Both ports are complex.
636 """
637 z01, z02 = 1-1j, 2+4j
638 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
639 a = rf.Network(name='a')
640 a.frequency = freq
641 a.s = np.random.default_rng().random(4).reshape(2, 2)
642 a.z0 = [z01, z02]
643 line = rf.media.DefinedGammaZ0(frequency=freq, z0=z02)
644 match_load = line.match(name='match_load')
645
646 # classic connecting
647 b = a ** match_load
648
649 # Circuit connecting
650 port1 = rf.Circuit.Port(freq, z0=z01, name='port1')
651 connections = [[(port1, 0), (a,0)], [(a, 1), (match_load, 0)]]
652 circuit = rf.Circuit(connections)
653
654 assert_array_almost_equal(b.s, circuit.s_external)
655
656 def test_2ports_default_characteristic_impedance(self):
657 """
658 Connect two 2-ports networks in a resulting 2-ports network,
659 same default charact impedance (50 Ohm) for all ports
660 """
661 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
662 a = rf.Network(name='a')
663 a.frequency = freq
664 a.s = np.random.default_rng().random(4).reshape(2, 2)
665
666 b = rf.Network(name='b')
667 b.frequency = freq
668 b.s = np.random.default_rng().random(4).reshape(2, 2)
669
670 # classic connecting
671 c = rf.connect(a, 1, b, 0)
672
673 # Circuit connecting
674 port1 = rf.Circuit.Port(freq, name='port1')
675 port2 = rf.Circuit.Port(freq, name='port2')
676
677 connections = [[(port1, 0), (a, 0)],
678 [(a, 1), (b, 0)],
679 [(b, 1), (port2, 0)]]
680 circuit = rf.Circuit(connections)
681
682 assert_array_almost_equal(c.s, circuit.s_external)
683
684 def test_2ports_complex_characteristic_impedance(self):
685 """
686 Connect two 2-ports networks in a resulting 2-ports network,
687 same complex charact impedance (1+1j) for all ports
688 """
689 z0 = 1 + 1j
690 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
691 a = rf.Network(name='a')
692 a.frequency = freq
693 a.s = np.random.default_rng().random(4).reshape(2, 2)
694 a.z0 = z0
695
696 b = rf.Network(name='b')
697 b.frequency = freq
698 b.s = np.random.default_rng().random(4).reshape(2, 2)
699 b.z0 = z0
700
701 # classic connecting
702 c = rf.connect(a, 1, b, 0)
703
704 # Circuit connecting
705 port1 = rf.Circuit.Port(freq, z0=z0, name='port1')
706 port2 = rf.Circuit.Port(freq, z0=z0, name='port2')
707
708 connections = [[(port1, 0), (a, 0)],
709 [(a, 1), (b, 0)],
710 [(b, 1), (port2, 0)]]
711 circuit = rf.Circuit(connections)
712
713 assert_array_almost_equal(c.s, circuit.s_external)
714
715 def test_2ports_different_characteristic_impedances(self):
716 """
717 Connect two 2-ports networks in a resulting 2-ports network,
718 different characteristic impedances for each network ports
719 """
720 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
721 a = rf.Network(name='a')
722 a.frequency = freq
723 a.s = np.random.default_rng().random(4).reshape(2,2)
724 a.z0 = [1, 2] # Z0 should never be zero
725
726 b = rf.Network(name='b')
727 b.frequency = freq
728 b.s = np.random.default_rng().random(4).reshape(2,2)
729 b.z0 = [11, 12]
730
731 # classic connecting
732 c = rf.connect(a, 1, b, 0)
733
734 # Circuit connecting
735 port1 = rf.Circuit.Port(freq, z0=1, name='port1')
736 port2 = rf.Circuit.Port(freq, z0=12, name='port2')
737
738 connections = [[(port1, 0), (a, 0)],
739 [(a, 1), (b, 0)],
740 [(b, 1), (port2, 0)] ]
741 circuit = rf.Circuit(connections)
742
743 assert_array_almost_equal(c.s, circuit.s_external)
744
745 def test_4ports_default_characteristic_impedances(self):
746 """
747 Connect two 4-ports networks in a resulting 4-ports network,
748 with default characteristic impedances
749 """
750 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
751 a = rf.Network(name='a')
752 a.frequency = freq
753 a.s = np.random.default_rng().random(16).reshape(4, 4)
754
755 b = rf.Network(name='b')
756 b.frequency = freq
757 b.s = np.random.default_rng().random(16).reshape(4, 4)
758
759 # classic connecting
760 c = rf.connect(a, 2, b, 0, 2)
761
762 # Circuit connecting
763 port1 = rf.Circuit.Port(freq, name='port1')
764 port2 = rf.Circuit.Port(freq, name='port2')
765 port3 = rf.Circuit.Port(freq, name='port3')
766 port4 = rf.Circuit.Port(freq, name='port4')
767
768 connections = [[(port1, 0), (a, 0)],
769 [(port2, 0), (a, 1)],
770 [(a, 2), (b, 0)],
771 [(a, 3), (b, 1)],
772 [(b, 2), (port3, 0)],
773 [(b, 3), (port4, 0)]]
774 circuit = rf.Circuit(connections)
775
776 assert_array_almost_equal(c.s, circuit.s_external)
777
778 def test_4ports_complex_characteristic_impedances(self):
779 """
780 Connect two 4-ports networks in a resulting 4-ports network,
781 with same complex characteristic impedances
782 """
783 z0 = 5 + 4j
784 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
785 a = rf.Network(name='a')
786 a.frequency = freq
787 a.s = np.random.default_rng().random(16).reshape(4, 4)
788 a.z0 = z0
789
790 b = rf.Network(name='b')
791 b.frequency = freq
792 b.s = np.random.default_rng().random(16).reshape(4, 4)
793 b.z0 = z0
794
795 # classic connecting
796 c = rf.connect(a, 2, b, 0, 2)
797
798 # circuit connecting
799 port1 = rf.Circuit.Port(freq, z0=z0, name='port1')
800 port2 = rf.Circuit.Port(freq, z0=z0, name='port2')
801 port3 = rf.Circuit.Port(freq, z0=z0, name='port3')
802 port4 = rf.Circuit.Port(freq, z0=z0, name='port4')
803
804 connections = [ [(port1, 0), (a, 0)],
805 [(port2, 0), (a, 1)],
806 [(a, 2), (b, 0)],
807 [(a, 3), (b, 1)],
808 [(b, 2), (port3, 0)],
809 [(b, 3), (port4, 0)]]
810 circuit = rf.Circuit(connections)
811
812 assert_array_almost_equal(c.s, circuit.s_external)
813
814 def test_4ports_different_characteristic_impedances(self):
815 """
816 Connect two 4-ports networks in a resulting 4-ports network,
817 with different characteristic impedances
818 """
819 z0 = [1, 2, 3, 4]
820 freq = rf.Frequency(start=1, npoints=1, unit='GHz')
821 a = rf.Network(name='a')
822 a.frequency = freq
823 a.s = np.random.default_rng().random(16).reshape(4, 4)
824 a.z0 = z0
825
826 b = rf.Network(name='b')
827 b.frequency = freq
828 b.s = np.random.default_rng().random(16).reshape(4, 4)
829 b.z0 = [11, 12, 13, 14]
830
831 # classic connecting
832 _c = rf.connect(a, 2, b, 0)
833 c = rf.innerconnect(_c, 2, 3)
834
835 # Circuit connecting
836 port1 = rf.Circuit.Port(freq, z0=1, name='port1')
837 port2 = rf.Circuit.Port(freq, z0=2, name='port2')
838 port3 = rf.Circuit.Port(freq, z0=13, name='port3')
839 port4 = rf.Circuit.Port(freq, z0=14, name='port4')
840
841 connections = [[(port1, 0), (a, 0)],
842 [(port2, 0), (a, 1)],
843 [(a, 2), (b, 0)],
844 [(a, 3), (b, 1)],
845 [(b, 2), (port3, 0)],
846 [(b, 3), (port4, 0)]]
847 circuit = rf.Circuit(connections)
848
849 assert_array_almost_equal(c.s, circuit.s_external)
850
851 def test_shunt_element(self):
852 """
853 Compare a shunt element network (here a capacitor)
854 """
855 freq = rf.Frequency(start=1, stop=2, npoints=101, unit='GHz')
856 line = rf.media.DefinedGammaZ0(frequency=freq, z0=50)
857 # usual way
858 cap_shunt_manual = line.shunt_capacitor(50e-12)
859
860 # A Circuit way
861 port1 = rf.Circuit.Port(frequency=freq, name='port1', z0=50)
862 port2 = rf.Circuit.Port(frequency=freq, name='port2', z0=50)
863 cap_shunt = line.capacitor(50e-12, name='cap_shunt')
864 ground = rf.Circuit.Ground(frequency=freq, name='ground', z0=50)
865
866 connections = [
867 [(port1, 0), (cap_shunt, 0), (port2, 0)],
868 [(cap_shunt, 1), (ground, 0)]
869 ]
870
871 # # Another possibility could have been without ground :
872 # shunt_cap = line.shunt_capacitor(50e-12)
873 # shunt_cap.name='shunt_cap'
874 # connections = [
875 # [(port1, 0), (shunt_cap, 0)],
876 # [(shunt_cap ,1), (port2, 0)]
877 # ]
878
879 cap_shunt_from_circuit = rf.Circuit(connections).network
880
881 assert_array_almost_equal(cap_shunt_manual.s, cap_shunt_from_circuit.s)
882
883 def test_reduce_circuit(self):
884 """
885 Test reducing multi-port cascade circuit
886 """
887 freq_n = 101
888 freq = rf.Frequency(start=1, stop=2, npoints=freq_n, unit='GHz')
889 line = rf.media.DefinedGammaZ0(frequency=freq, z0=50)
890
891 # network A
892 ntwkA = rf.Network(name='a')
893 ntwkA.frequency = freq
894 ntwkA_z0 = (60, 70, 80, 90, 100)
895 ntwkA_np = len(ntwkA_z0)
896 ntwkA.z0 = [ntwkA_z0]*freq_n
897 ntwkA.s = np.random.default_rng().random(freq_n * ntwkA_np**2).reshape(freq_n, ntwkA_np, ntwkA_np)
898
899 # network B
900 ntwkB = rf.Network(name='b')
901 ntwkB.frequency = freq
902 ntwkB_z0 = (10, 20, 30, 40, 50)
903 ntwkB_np = len(ntwkB_z0)
904 ntwkB.z0 = [ntwkB_z0]*freq_n
905 ntwkB.s = np.random.default_rng().random(freq_n * ntwkB_np**2).reshape(freq_n, ntwkB_np, ntwkB_np)
906
907 # Construct the connection
908 port1 = rf.Circuit.Port(frequency=freq, name='port1', z0=50)
909 port2 = rf.Circuit.Port(frequency=freq, name='port2', z0=50)
910 ground1 = rf.Circuit.Ground(frequency=freq, name='ground1', z0=50)
911 ground2 = rf.Circuit.Ground(frequency=freq, name='ground2', z0=50)
912 open_port = rf.Circuit.Open(frequency=freq, name='open_port', z0=50)
913 ind_shunt = line.inductor(50e-12, name='ind_shunt')
914 cap_series = line.capacitor(30e-9, name='cap_series')
915 port3 = rf.Circuit.Port(frequency=freq, name='port3', z0=50)
916 port4 = rf.Circuit.Port(frequency=freq, name='port4', z0=50)
917
918 connections = [
919 [(ntwkA, 0), (cap_series, 0)],
920 [(port1, 0), (ind_shunt, 0), (cap_series, 1)],
921 [(ground1, 0), (ind_shunt, 1)],
922 [(ntwkA, 1), (port2, 0)],
923 [(ntwkA, 2), (ground2, 0)],
924 [(ntwkA, 3), (open_port, 0)],
925 [(ntwkA, 4), (ntwkB, 0)],
926 [(ntwkB, 1), (port3, 0)],
927 [(ntwkB, 2), (port4, 0)],
928 [(ntwkB, 3), (ntwkB, 4)],
929 ]
930
931 # Circuit connecting
932 circuit = rf.Circuit(connections)
933
934 # Reduce the circuit
935 reduced_cnxs = rf.reduce_circuit(connections)
936 reduced_circuit = rf.Circuit(reduced_cnxs)
937
938 assert_array_almost_equal(circuit.network.s, reduced_circuit.network.s)
939
940 class CircuitTestVariableCoupler(unittest.TestCase):
941 """
942 If we use 3 dB hybrid defined as :
943 ________
944 Input 0 --| |-- 1 Through
945 | |
946 Isolated 3 --|_______|-- 2 Coupled
947
948 And if we connect two 3 dB hybrid with a phase shifter such as:
949 _________ _________
950 Port#3 >- 0-| |-1 -------------------------- 0-| |-1 -< Port#2
951 | Hyb1 | | Hyb2 |
952 Port#0 >- 3-|________|-2 -- 0-[phase shifter]-1 -- 3-|________|-2 -< Port#1
953
954 Then we have a variable coupler. The coupling factor can be adjusted
955 by changing the phase of the phase shifter.
956
957 The port order in this example is voluntary complicated to make a good
958 example.
959
960 """
961 def setUp(self):
962 self.freq = rf.Frequency(start=1.5, stop=1.5, npoints=1, unit='GHz')
963 self.coax = rf.media.DefinedGammaZ0(frequency=self.freq)
964 self.test_dir = os.path.dirname(os.path.abspath(__file__))+'/'
965
966 def phase_shifter(self, phase_deg):
967 return self.coax.line(d=phase_deg, unit='deg')
968
969 def hybrid(self, name='hybrid'):
970 Sc = 1/np.sqrt(2)*np.array([[ 0, 1, 1j, 0],
971 [ 1, 0, 0, 1j],
972 [1j, 0, 0, 1],
973 [ 0, 1j, 1, 0]])
974 hybrid = rf.Network(frequency=self.freq, s=Sc, name=name)
975 return hybrid
976
977 def variable_coupler_network_from_connect(self, phase_deg):
978 ps = self.phase_shifter(phase_deg)
979 hybrid1, hybrid2 = self.hybrid(), self.hybrid()
980 # connecting the networks together.
981 # NB: in order to know which port corresponds to which, it is useful
982 # to define different port characteristic impedances like
983 # hybrid1.z0 = [11, 12, 13, 14]
984 # hybrid2.z0 = [21, 22, 23, 24]
985 # ps.z0 = [31, 32]
986 # and to make a drawing of each steps.
987 # This is not convenient, that's why the Circuit approach can be easier.
988 _temp = rf.connect(hybrid1, 2, ps, 0)
989 _temp = rf.connect(_temp, 1, hybrid2, 0)
990 _temp = rf.innerconnect(_temp, 1, 5)
991 # re-order port numbers to match the example
992 _temp.renumber([0, 1, 2, 3], [3, 0, 2, 1])
993 return _temp
994
995 def variable_coupler_circuit(self, phase_deg):
996 ps = self.phase_shifter(phase_deg)
997 ps.name = 'ps' # do not forget the name of the network !
998 hybrid1, hybrid2 = self.hybrid('hybrid1'), self.hybrid('hybrid2')
999
1000 port1 = rf.Circuit.Port(ps.frequency, 'port1')
1001 port2 = rf.Circuit.Port(ps.frequency, 'port2')
1002 port3 = rf.Circuit.Port(ps.frequency, 'port3')
1003 port4 = rf.Circuit.Port(ps.frequency, 'port4')
1004 # Note that the order of port appearance is important.
1005 # 1st port to appear in the connection setup will be the 1st port (0),
1006 # then second to appear the second port (1), etc...
1007 # There is no constraint for the order of the connections.
1008 connections = [
1009 [(port1, 0), (hybrid1, 3)],
1010 [(port2, 0), (hybrid2, 2)],
1011 [(port3, 0), (hybrid2, 1)],
1012 [(port4, 0), (hybrid1, 0)],
1013 [(hybrid1, 2), (ps, 0)],
1014 [(hybrid1, 1), (hybrid2, 0)],
1015 [(ps, 1), (hybrid2, 3)],
1016 ]
1017
1018 return rf.Circuit(connections)
1019
1020 def variable_coupler_network_from_circuit(self, phase_deg):
1021 return self.variable_coupler_circuit(phase_deg).network
1022
1023 def test_compare_with_network_connect(self):
1024 """
1025 Compare with the S-parameters obtained from Network.connect
1026 """
1027 phase_deg = np.random.default_rng().integers(low=0, high=180)
1028 vc_connect = self.variable_coupler_network_from_connect(phase_deg)
1029 vc_circuit = self.variable_coupler_network_from_circuit(phase_deg)
1030 assert_array_almost_equal(vc_connect.s, vc_circuit.s)
1031
1032 def test_compare_with_designer(self):
1033 """
1034 Compare with the S-parameters obtained from ANSYS Designer
1035 """
1036 for phase_angle in [20, 75]:
1037 vc_designer = rf.Network(os.path.join(self.test_dir,
1038 f"designer_variable_coupler_ideal_{phase_angle}deg.s4p"))
1039 vc_circuit = self.variable_coupler_network_from_circuit(phase_angle)
1040 assert_array_almost_equal(vc_designer.s, vc_circuit.s, decimal=4)
1041
1042 def test_compare_connect_and_designer(self):
1043 """
1044 Compare S-parameters obtained from ANSYS Designer with Network.connect
1045 """
1046 for phase_angle in [20, 75]:
1047 vc_designer = rf.Network(os.path.join(self.test_dir,
1048 f"designer_variable_coupler_ideal_{phase_angle}deg.s4p"))
1049 vc_connect = self.variable_coupler_network_from_connect(phase_angle)
1050 assert_array_almost_equal(vc_designer.s, vc_connect.s, decimal=4)
1051
1052
1053 class CircuitTestGraph(unittest.TestCase):
1054 """
1055 Test functionalities linked to graph method, used in particular for plotting
1056 """
1057
1058 def setUp(self):
1059 """
1060 Dummy Circuit setup
1061
1062 Setup a circuit which has various interconnections (2 or 3)
1063 """
1064 self.freq = rf.Frequency(start=1, stop=2, npoints=101, unit='GHz')
1065
1066 # dummy components
1067 self.R = 100
1068 self.line_resistor = rf.media.DefinedGammaZ0(frequency=self.freq, z0=self.R)
1069 resistor1 = self.line_resistor.resistor(self.R, name='resistor1')
1070 resistor2 = self.line_resistor.resistor(self.R, name='resistor2')
1071 resistor3 = self.line_resistor.resistor(self.R, name='resistor3')
1072 port1 = rf.Circuit.Port(self.freq, name='port1')
1073
1074 # Connection setup
1075 self.connections = [
1076 [(port1, 0), (resistor1, 0), (resistor3, 0)],
1077 [(resistor1, 1), (resistor2, 0)],
1078 [(resistor2, 1), (resistor3, 1)]
1079 ]
1080
1081 self.C = rf.Circuit(self.connections)
1082
1083 def test_intersection_dict(self):
1084 inter_dict = self.C.intersections_dict
1085 # should have 3 intersections
1086 self.assertTrue(len(inter_dict) == 3)
1087 # All intersections should have at least 2 edges
1088 for it in inter_dict.items():
1089 k, cnx = it
1090 self.assertTrue(len(cnx) >= 2)
1091
1092 def test_edge_labels(self):
1093 edge_labels = self.C.edge_labels
1094 self.assertTrue(len(edge_labels) == 7)
1095
1096
1097 class CircuitTestComplexCharacteristicImpedance(unittest.TestCase):
1098 """
1099 Test creating circuits with real and complex port charac.impedances
1100 """
1101 def setUp(self):
1102 self.f0 = rf.Frequency(75.8, npoints=1, unit='GHz')
1103 # initial s-param values of A 2 ports network
1104 self.s0 = np.array([ # dummy values
1105 [-0.1000 -0.2000j, -0.3000 +0.4000j],
1106 [-0.3000 +0.4000j, 0.5000 -0.6000j]]).reshape(-1,2,2)
1107
1108 # build initial network (under z0=50)
1109 self.ntw0 = rf.Network(frequency=self.f0, s=self.s0, z0=50, name='dut')
1110
1111 # complex characteristic impedance to renormalize to
1112 self.zdut = 100 + 10j
1113
1114 # reference solutions obtained from ANSYS Circuit or ADS (same res)
1115 # when z0=[50, zdut]
1116 self.z_ref = np.array([ # not affected by z0
1117 [18.0000 -16.0000j, 20.0000 +40.0000j],
1118 [20.0000 +40.0000j, 10.0000 -80.0000j]]).reshape(-1,2,2)
1119
1120 self.y_ref = np.array([ # not affected by z0
1121 [0.0251 +0.0023j, 0.0123 -0.0066j],
1122 [0.0123 -0.0066j, 0.0052 +0.0055j]]).reshape(-1,2,2)
1123
1124 self.s_ref = np.array([ # renormalized s (power-waves)
1125 [-0.1374 -0.2957j, -0.1995 +0.5340j],
1126 [-0.1995 +0.5340j, -0.0464 -0.7006j]]).reshape(-1,2,2)
1127
1128 # Creating equivalent reference circuit
1129 port1 = rf.Circuit.Port(self.f0, z0=50, name='port1')
1130 port2 = rf.Circuit.Port(self.f0, z0=50, name='port2')
1131 ntw0 = self.ntw0
1132
1133 cnx = [ # z0=[50,50]
1134 [(port1, 0), (ntw0, 0)],
1135 [(ntw0, 1), (port2, 0)]
1136 ]
1137 self.cir = rf.Circuit(cnx)
1138
1139 # Creating equivalent circuit with z0 real
1140 port2_real = rf.Circuit.Port(self.f0, z0=100, name='port2')
1141 cnx_real = [ # z0=[50,100]
1142 [(port1, 0), (ntw0, 0)],
1143 [(ntw0, 1), (port2_real, 0)]
1144 ]
1145 self.cir_real = rf.Circuit(cnx_real)
1146
1147 # Creating equivalent circuit with z0 complex
1148 port2_complex = rf.Circuit.Port(self.f0, z0=self.zdut, name='port2')
1149 cnx_complex = [ # z0=[50,zdut]
1150 [(port1, 0), (ntw0, 0)],
1151 [(ntw0, 1), (port2_complex, 0)]
1152 ]
1153 self.cir_complex = rf.Circuit(cnx_complex)
1154
1155 # references for each s-param definition
1156 self.s_legacy = rf.renormalize_s(self.s0, [50,50], [50,self.zdut],
1157 s_def='traveling')
1158 self.s_power = rf.renormalize_s(self.s0, [50,50], [50,self.zdut],
1159 s_def='power')
1160 self.s_pseudo = rf.renormalize_s(self.s0, [50,50], [50,self.zdut],
1161 s_def='pseudo')
1162 # for real values, should be = whatever s-param definition
1163 self.s_real = rf.renormalize_s(self.s0, [50,50], [50,100])
1164
1165 def test_verify_reference(self):
1166 ' Check that the reference results comes from power-waves definition'
1167 np.testing.assert_allclose(self.s_ref, self.s_power, atol=1e-4)
1168
1169 def test_reference_s(self):
1170 ' Check reference z0 circuit, just in case '
1171 np.testing.assert_allclose(self.cir.network.s, self.s0, atol=1e-4)
1172
1173 def test_real_z0_s(self):
1174 ' Check real z0 circuit '
1175 np.testing.assert_allclose(self.cir_real.network.s, self.s_real, atol=1e-4)
1176
1177 def test_real_z_params(self):
1178 ' Check Z-parameters match'
1179 np.testing.assert_allclose(self.cir.network.z, self.cir_real.network.z, atol=1e-4)
1180
1181 def test_complex_z_params(self):
1182 ' Check Z-parameters match'
1183 np.testing.assert_allclose(self.cir_complex.network.z, self.cir_real.network.z, atol=1e-4)
1184
1185 @unittest.expectedFailure
1186 def test_complexz0_s_vs_legacy(self):
1187 ' Check complex z0 circuit vs legacy renormalization '
1188 np.testing.assert_allclose(self.cir_complex.network.s, self.s_legacy, atol=1e-4)
1189
1190 def test_complexz0_s_vs_powerwaves(self):
1191 ' Check complex z0 circuit vs power-waves renormalization '
1192 np.testing.assert_allclose(self.cir_complex.network.s, self.s_ref, atol=1e-4)
1193 np.testing.assert_allclose(self.cir_complex.network.s, self.s_power, atol=1e-4)
1194
1195 @unittest.expectedFailure
1196 def test_complexz0_s_vs_pseudo(self):
1197 ' Check complex z0 circuit vs pseudo-waves renormalization '
1198 np.testing.assert_allclose(self.cir_complex.network.s, self.s_pseudo, atol=1e-4)
1199
1200 class CircuitTestVoltagesCurrents(unittest.TestCase):
1201 def setUp(self):
1202 rng = np.random.default_rng()
1203 # setup a test transmission line randomly excited
1204 self.P_f = rng.random() # forward power in Watt
1205 self.phase_f = rng.random() # forward phase in rad
1206 self.Z = rng.random() # source internal impedance, line characteristic impedance and load impedance
1207 self.L = rng.random() # line length in [m]
1208 self.L2 = rng.random() # line length in [m]
1209 self.L3 = rng.random() # line length in [m]
1210 self.freq = rf.Frequency(1, 10, 10, unit='GHz')
1211 self.line_media = rf.media.DefinedGammaZ0(self.freq, z0=self.Z) # lossless line medium
1212 self.line = self.line_media.line(d=self.L, unit='m', name='line') # transmission line Network
1213 self.line2 = self.line_media.line(d=self.L2, unit='m', name='line2') # transmission line Network
1214 self.line3 = self.line_media.line(d=self.L3, unit='m', name='line3') # transmission line Network
1215 self.tee = self.line_media.tee(name='tee')
1216 self.resistor = self.line_media.resistor(50, name='resistor')
1217
1218 # forward voltages and currents at the input of the test line
1219 self.V_in = np.sqrt(2*self.Z*self.P_f)*np.exp(1j*self.phase_f)
1220 self.I_in = np.sqrt(2*self.P_f/self.Z)*np.exp(1j*self.phase_f)
1221 # forward voltages and currents at the output of the test line
1222 theta = rf.theta(self.line_media.gamma, self.freq.f, self.L) # electrical length
1223 self.V_out, self.I_out = rf.tlineFunctions.voltage_current_propagation(self.V_in, self.I_in, self.Z, theta)
1224
1225 # Equivalent model with Circuit
1226 self.port1 = rf.Circuit.Port(frequency=self.freq, name='port1', z0=self.Z)
1227 self.port2 = rf.Circuit.Port(frequency=self.freq, name='port2', z0=self.Z)
1228 cnx = [
1229 [(self.port1, 0), (self.line, 0)],
1230 [(self.port2, 0), (self.line, 1)]
1231 ]
1232 self.crt = rf.Circuit(cnx)
1233 # power and phase arrays for Circuit.voltages() and currents()
1234 self.power = [self.P_f, 0]
1235 self.phase = [self.phase_f, 0]
1236
1237 def test_tline_voltages(self):
1238 ' Test voltages for a simple transmission line '
1239 V_ports = self.crt.voltages_external(self.power, self.phase)
1240
1241 np.testing.assert_allclose(self.V_in, V_ports[:,0])
1242 np.testing.assert_allclose(self.V_out, V_ports[:,1])
1243
1244 def test_tline_currents(self):
1245 ' Test currents for a simple transmission line '
1246 I_ports = self.crt.currents_external(self.power, self.phase)
1247
1248 np.testing.assert_allclose(self.I_in, I_ports[:,0])
1249 # output current is * -1 as Circuit definition is opposite
1250 # (toward the Circuit's Port)
1251 np.testing.assert_allclose(self.I_out, -1*I_ports[:,1])
1252
1253 def test_multiple_ports(self):
1254 ' Test voltages and currents for a connection with multiple ports '
1255 # Create a circuit build with tee
1256 cnx_with_tee = [
1257 [(self.port1, 0), (self.line, 0)],
1258 [(self.line, 1), (self.tee, 0)],
1259 [(self.line2, 0), (self.tee, 1)],
1260 [(self.line3, 0), (self.tee, 2)],
1261 [(self.line2, 1), (self.port2, 0)],
1262 [(self.line3, 1), (self.resistor, 0)],
1263 ]
1264
1265 # Create a circuit build without tee
1266 cnx = [
1267 [(self.port1, 0), (self.line, 0)],
1268 [(self.line, 1), (self.line2, 0), (self.line3, 0)],
1269 [(self.line2, 1), (self.port2, 0)],
1270 [(self.line3, 1), (self.resistor, 0)],
1271 ]
1272
1273 # Create the two circuits
1274 crt_with_tee = rf.Circuit(cnx_with_tee)
1275 crt = rf.Circuit(cnx)
1276
1277 # Get voltages and currents for both circuits
1278 V_with_tee = crt_with_tee.voltages(self.power, self.phase)
1279 I_with_tee = crt_with_tee.currents(self.power, self.phase)
1280
1281 V = crt.voltages(self.power, self.phase)
1282 I = crt.currents(self.power, self.phase)
1283
1284 # Get the reordering of the ports in the two circuits
1285 port_order_with_tee = (0, 1, 2, 4, 6, 8, 9, 10, 11)
1286 port_order = (0, 1, 2, 3, 4, 5, 6, 7, 8)
1287
1288 for v, v_with_tee in zip((V, I), (V_with_tee, I_with_tee)):
1289 v_with_tee_reordered = np.array(
1290 [v_with_tee[:, i] for i in port_order_with_tee]
1291 )
1292 v_reordered = np.array([v[:, i] for i in port_order])
1293
1294 np.testing.assert_allclose(v_with_tee_reordered, v_reordered)
1295
1296 def test_tline_with_different_impedance(self):
1297 ' Test voltages and currents for a simple transmission line with different impedances '
1298 line = self.line.copy()
1299 line.renormalize(z_new=1./self.Z)
1300
1301 # Equivalent model with Circuit
1302 cnx = [
1303 [(self.port1, 0), (line, 0)],
1304 [(self.port2, 0), (line, 1)]
1305 ]
1306 crt = rf.Circuit(cnx)
1307
1308 V_ports_uni_z = crt.voltages(self.power, self.phase)
1309 I_ports_uni_z = crt.currents(self.power, self.phase)
1310
1311 V_ports_dif_z = self.crt.voltages(self.power, self.phase)
1312 I_ports_dif_z = self.crt.currents(self.power, self.phase)
1313
1314 np.testing.assert_allclose(I_ports_uni_z, I_ports_dif_z)
1315 np.testing.assert_allclose(V_ports_uni_z, V_ports_dif_z)
1316
1317 class CircuitTestVoltagesNonReciprocal(unittest.TestCase):
1318 def test_isolator(self):
1319 # Isolator that passes from port 1 to 2
1320 freq = rf.Frequency.from_f([1], unit='GHz')
1321 s = np.array([[[0, 0], [1, 0]]])
1322 network = rf.Network(frequency=freq, s=s, name='isolator')
1323 port1 = rf.Circuit.Port(frequency=freq, name='port1', z0=50)
1324 port2 = rf.Circuit.Port(frequency=freq, name='port2', z0=50)
1325 cnx = [
1326 [(network,0),(port1,0)],
1327 [(network,1),(port2,0)]
1328 ]
1329 crt = rf.Circuit(cnx)
1330 np.testing.assert_allclose(crt.s_external, s)
1331
1332 power = [1,0] # 1 Watt at port 1
1333 phase = [0,0]
1334 V_at_ports = crt.voltages_external(power, phase)
1335 I_at_ports = crt.currents_external(power, phase)
1336 np.testing.assert_allclose(V_at_ports, [[10+0j, 10+0j]])
1337 # Positive current entering into port
1338 np.testing.assert_allclose(I_at_ports, [[0.2+0j, -0.2+0j]])
1339
1340 power = [0,1] # 1 Watt at port 2
1341 V_at_ports = crt.voltages_external(power, phase)
1342 I_at_ports = crt.currents_external(power, phase)
1343 np.testing.assert_allclose(V_at_ports, [[0+0j, 10+0j]])
1344 np.testing.assert_allclose(I_at_ports, [[0+0j, 0.2+0j]])
1345
1346 def test_isolator_reverse(self):
1347 # Isolator that passes from port 2 to 1
1348 freq = rf.Frequency.from_f([1], unit='GHz')
1349 s = np.array([[[0, 1], [0, 0]]])
1350 network = rf.Network(frequency=freq, s=s, name='isolator')
1351 port1 = rf.Circuit.Port(frequency=freq, name='port1', z0=50)
1352 port2 = rf.Circuit.Port(frequency=freq, name='port2', z0=50)
1353 cnx = [
1354 [(network,0),(port1,0)],
1355 [(network,1),(port2,0)]
1356 ]
1357 crt = rf.Circuit(cnx)
1358 np.testing.assert_allclose(crt.s_external, s)
1359
1360 power = [1,0] # 1 Watt at port 1
1361 phase = [0,0]
1362 V_at_ports = crt.voltages_external(power, phase)
1363 I_at_ports = crt.currents_external(power, phase)
1364 np.testing.assert_allclose(V_at_ports, [[10+0j, 0+0j]])
1365 np.testing.assert_allclose(I_at_ports, [[0.2+0j, 0+0j]])
1366
1367 power = [0,1] # 1 Watt at port 2
1368 V_at_ports = crt.voltages_external(power, phase)
1369 I_at_ports = crt.currents_external(power, phase)
1370 np.testing.assert_allclose(V_at_ports, [[10+0j, 10+0j]])
1371 np.testing.assert_allclose(I_at_ports, [[-0.2+0j, 0.2+0j]])
1372
1373 if __name__ == '__main__':
1374 unittest.main()