def test_RC1(self): """Lcapy: check RC network """ a = Circuit() a.add('R1 1 2') a.add('C1 2 0') self.assertEqual2(a.impedance(1, 2), R('R1').impedance, "Z incorrect for R1.") self.assertEqual2(a.impedance(2, 0), C('C1').impedance, "Z incorrect for C1.") self.assertEqual2(a.impedance(1, 0), (R('R1') + C('C1')).impedance, "Z incorrect for R1 + C1.") self.assertEqual2(a.admittance(1, 2), R('R1').Y, "Y incorrect for R1.") self.assertEqual2(a.admittance(2, 0), C('C1').Y, "Y incorrect for C1.") self.assertEqual2(a.admittance(1, 0), (R('R1') + C('C1')).Y, "Y incorrect for R1 + C1.") self.assertEqual2(a.Isc(1, 0), I(0).Isc, "Isc incorrect") self.assertEqual(a.R1.Z, expr('R1'), "Z incorrect") self.assertEqual(a.R1.R, expr('R1'), "R incorrect") self.assertEqual(a.R1.X, 0, "X incorrect") self.assertEqual(a.C1.Y, expr('j * omega * C1'), "Y incorrect") self.assertEqual(a.C1.G, 0, "G incorrect") self.assertEqual(a.C1.B, expr('-omega * C1'), "B incorrect")
def test_VRC1(self): """Lcapy: check VRC circuit """ a = Circuit() a.add('V1 1 0 {V1 / s}') a.add('R1 1 2') a.add('C1 2 0') # Note, V1 acts as a short-circuit for the impedance/admittance self.assertEqual2(a.impedance(1, 2), (R('R1') | C('C1')).Z, "Z incorrect across R1") self.assertEqual2(a.impedance(2, 0), (R('R1') | C('C1')).Z, "Z incorrect across C1") self.assertEqual2(a.impedance(1, 0), R(0).Z, "Z incorrect across V1") self.assertEqual2(a.admittance(1, 2), (R('R1') | C('C1')).Y, "Y incorrect across R1") self.assertEqual2(a.admittance(2, 0), (R('R1') | C('C1')).Y, "Y incorrect across C1") # This has a non-invertible A matrix. # self.assertEqual2(a.admittance(1, 0), R(0).Y, "Y incorrect across V1") self.assertEqual2( a.Voc(1, 0).s, V('V1 / s').Voc.s, "Voc incorrect across V1") self.assertEqual(a.is_ivp, False, "Initial value problem incorrect") self.assertEqual(a.is_dc, False, "DC incorrect") self.assertEqual(a.is_ac, False, "AC incorrect")
def test_resistance(self): Rexe = 0.0000001 Rexc = 0.0000001 Rwe = 0.2545383947014702 Rwke = 0.7943030881649811 Rv = 8.852701208752846e-06 Rintere = 0.00034794562965549745 Rinterc = 0.00017397281482774872 Rwkc = 0.39715154408249054 Rwka = 744.3007160198263 Rwa = 456.90414284754644 Rwc = 0.1272691973507351 Rtot = (R(Rexe) + (R(Rwa) | R(Rwe) + (R(Rwka)|R(Rwke)+R(Rintere)+R(Rv)+R(Rinterc)+R(Rwkc))+ R(Rwc))+ R(Rexc)) print(Rtot.simplify()) ans = 16731692103737332239244353077427184638278095509511778941./10680954190791611228174081719413008273307025000000000000. Rtot2 = (self.prob.get_val('circ.n1.T')-self.prob.get_val('circ.n8.T'))/self.prob.get_val('circ.Rex_c.q') assert_near_equal(Rtot2, ans, tolerance=1.0E-5) draw = False # plot the thermal network if draw: Rtot.draw('Thermal_Network.pdf')
def test_resistance(self): Rtot = (R(1) + (R(2) + R(2) | R(3) + (R(4) | R(5) + R(5) + R(5) + R(5)))) print(Rtot.simplify()) #assert_near_equal(self.prob.get_val('ohp.mass_OHP'), 385.90453402, tolerance=1.0E-5) draw = False # plot the thermal network if draw: Rtot.draw('Thermal_Network.pdf')
def load(self, V_load): # Predefine instance attributes self.R_load = None self.I_load = None self.P_load = None self.V_load = V_load Load = self.Load if type(Load) in [int, float] or Load.is_same_unit(1 @ u_Ohm): Load = self.Load = Load @ u_Ohm self.R_load = Load self.I_load = V_load / self.R_load if Load.is_same_unit(1 @ u_W): self.P_load = Load self.I_load = self.P_load / V_load else: if Load.is_same_unit(1 @ u_A): self.I_load = Load self.P_load = V_load * self.I_load if not self.R_load: self.R_load = V_load / self.I_load if not hasattr(self, 'Z_load'): self.Z_load = R(self.R_load)
def test_RC1(self): """Lcapy: check RC network """ a = Circuit() a.add('R1 1 2') a.add('C1 2 0') self.assertEqual2(a.Z(1, 2), R('R1').Z, "Z incorrect for R1.") self.assertEqual2(a.Z(2, 0), C('C1').Z, "Z incorrect for C1.") self.assertEqual2( a.Z(1, 0), (R('R1') + C('C1')).Z, "Z incorrect for R1 + C1.") self.assertEqual2(a.Y(1, 2), R('R1').Y, "Y incorrect for R1.") self.assertEqual2(a.Y(2, 0), C('C1').Y, "Y incorrect for C1.") self.assertEqual2( a.Y(1, 0), (R('R1') + C('C1')).Y, "Y incorrect for R1 + C1.") self.assertEqual2(a.Isc(1, 0), I(0).Isc, "Isc incorrect")
def test_VR_ac1(self): """Lcapy: check VR ac network """ a = Vac(4) | R(2) self.assertEqual(a.is_dc, False, "DC incorrect") self.assertEqual(a.is_ac, True, "AC incorrect") self.assertEqual(a.i, 0, "Open circuit terminal current not zero")
def test_VRL1(self): """Lcapy: check VRL circuit """ a = Circuit() a.add('V1 1 0 {V1 / s}') a.add('R1 1 2') a.add('L1 2 0 L1 0') # This currently fails due to two symbols of the same name # having different assumptions. # Note, V1 acts as a short-circuit for the impedance/admittance self.assertEqual2( a.thevenin(1, 2).Voc, a.Voc(1, 2), "incorrect thevenin voltage") self.assertEqual2( a.thevenin(1, 2).Z, a.impedance(1, 2), "incorrect thevenin impedance") self.assertEqual2( a.norton(1, 2).Isc, a.Isc(1, 2), "incorrect norton current") self.assertEqual2( a.norton(1, 2).Y, a.admittance(1, 2), "incorrect norton admittance") self.assertEqual2(a.impedance(1, 2), (R('R1') | L('L1')).Z, "Z incorrect across R1") self.assertEqual2(a.impedance(2, 0), (R('R1') | L('L1')).Z, "Z incorrect across L1") self.assertEqual2(a.impedance(1, 0), R(0).Z, "Z incorrect across V1") self.assertEqual2(a.admittance(1, 2), (R('R1') | L('L1')).Y, "Y incorrect across R1") self.assertEqual2(a.admittance(2, 0), (R('R1') | L('L1')).Y, "Y incorrect across L1") # This has a non-invertible A matrix. # self.assertEqual2(a.admittance(1, 0), R(0).Y, "Y incorrect across V1") self.assertEqual2(a.Voc(1, 0), V('V1' / s).Voc, "Voc incorrect across V1") self.assertEqual(a.is_ivp, True, "Initial value problem incorrect") self.assertEqual(a.is_dc, False, "DC incorrect") self.assertEqual(a.is_ac, False, "AC incorrect")
def test_VR_dc1(self): """Lcapy: check VR dc network """ a = Vdc(4) | R(2) self.assertEqual(a.initial_value_problem, False, "initial_value_problem incorrect") self.assertEqual(a.is_ac, False, "AC incorrect") self.assertEqual(a.is_dc, True, "DC incorrect") self.assertEqual(a.i, 0, "Open circuit terminal current not zero")
from lcapy import R, C, L, Par N = Par(R(1), R(2), R(3)) N.draw('par3.png')
from lcapy import Vdc, R, L, C, LSection, Shunt import numpy as np from matplotlib.pyplot import figure, savefig, show a1 = LSection(L(10), C(1, 5)) b1 = a1.prepend(Shunt(Vdc(5))).load(R(5)) a2 = LSection(L(10), C(1e-10, 5)) b2 = a2.prepend(Shunt(Vdc(5))).load(R(5)) a1 = (Vdc(5) + L(10)) a2 = (Vdc(5) + L(10)) | C(1, 5) b1 = a1.load(R(5)) b2 = a2.load(R(5)) t = np.linspace(0, 10, 1000) fig = figure() ax = fig.add_subplot(111) # Voltage across R ax.plot(t, b1.V.impulse_response(t), linewidth=2, label='without C') ax.plot(t, b2.V.impulse_response(t), linewidth=2, label='with C') ax.legend() ax.set_xlabel('Time (s)') ax.set_ylabel('Voltage (V)') ax.grid(True) fig = figure() ax = fig.add_subplot(111) # Current through R ax.plot(t, b1.I.impulse_response(t), linewidth=2, label='without C')
from lcapy import R, C n = C('C1') | (R('R1') + (C('C2') | (R('R2') + (C('C3') | R('R3'))))) n.draw(__file__.replace('.py', '.png'), form='ladder')
from lcapy import R, C, L ((R(1) + L(2)) | C(3)).smodel().draw('pickup-s.png')
from lcapy import Vdc, R, L, C import numpy as np from matplotlib.pyplot import figure, savefig, show a = (Vdc(5) + L(10)) | R(5) b = Vdc(5) + L(10) + R(5) t = np.linspace(0, 10, 1000) fig = figure() ax = fig.add_subplot(111) # Voltage across R ax.plot(t, a.V.impulse_response(t), linewidth=2) ax.set_xlabel('Time (s)') ax.set_ylabel('Voltage (V)') ax.grid(True) fig = figure() ax = fig.add_subplot(111) # Current through R ax.plot(t, b.I.impulse_response(t), linewidth=2) ax.set_xlabel('Time (s)') ax.set_ylabel('Current (A)') ax.grid(True) show()
from lcapy import R, C, L n = C('C1') | ((R('R1') + L('L1')) + (C('C2') | ((R('R2') + L('L2')) + (C('C3') | (R('R3') + L('L3')) + C('C4'))))) n.draw(__file__.replace('.py', '.png'), layout='ladder')
def network(self): return LSection(C('C_block'), R('R_shunt'))
from lcapy import Opamp, R, C, Series import numpy as np from matplotlib.pyplot import figure, savefig, show # Create simple differentiator Ci = 1e-6 Rf = 1e3 a = Opamp() # Connect V+ to ground. b = a.short_circuit(1) # Add feedback resistor. c = b.bridge(R(Rf)) # Add input capacitor to V-. d = c.prepend(Series(C(Ci))) print d.Vtransfer f = np.logspace(1, 8, 1000) fig = figure() ax = fig.add_subplot(111) Zf = d.Vtransfer.frequency_response(f) ax.loglog(f, abs(Zf)) ax.grid(True) show()
from lcapy import R R1 = R(10) R2 = R(5) Rtot = R1 + R2 Rtot.draw('rseries.png')
S = 310e-4 Bl = 10.0 omegas = 2 * np.pi * fs Rms = Qms / (omegas * Cms) Rs = Bl**2 / Rms Ls = Bl**2 * Cms Cs = Mms / (Bl**2) print Rms print Rs print Ls print Cs a = Series(R(Re) + L(Le)).chain(IdealGyrator(Bl)) b = a.chain(Series(R(Rms) + L(Mms) + C(Cms))) f = np.logspace(0, np.log10(20e3), 2000) Zin = b.short_circuit(2).Z.frequency_response(f) fig = figure() ax = fig.add_subplot(111) #ax.semilogx(f, abs(Zin), linewidth=2) ax.semilogx(f, Zin.real, linewidth=2) #ax.semilogx(f, Zin.imag, linewidth=2) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('Resistance (ohms)') ax.grid(True) fig = figure()
from lcapy import R, L n = (R('R1') | L('L1')) + (R('R2') | L('L2')) n.draw(__file__.replace('.py', '.png'))
from lcapy import Istep, R, L, C from matplotlib.pyplot import figure, savefig, show import numpy as np a = Istep(10) | R(0.1) | C(0.4) | L(0.2) a.Voc.pprint() t = np.linspace(0, 10, 1000) fig = figure() ax = fig.add_subplot(111) ax.plot(t, a.Voc.transient_response(t), linewidth=2) ax.set_xlabel('Time (s)') ax.set_ylabel('Voltage (V)') ax.grid(True) savefig('parallel-IRLC1-voc.png') show()
from lcapy import Opamp, R, Series # Create simple non-inverting amplifier Rf = 1000 R1 = 50 a = Opamp() # Add feedback resistor. b = a.bridge(R(Rf), 2, 3) # Connect R1 to ground across port 2. c = b.parallel(R(R1), port=2) print c.Vgain(1, 3)
from lcapy import R N = (R(1) + R(2) + R(3)) | (R(4) + R(5)) N.draw('net1.png', label_values=True)
from lcapy import Vstep, R, L, C import numpy as np from matplotlib.pyplot import figure, savefig, show # This is posed as an initial value problem so cannot # determine result for t < 0. a1 = Vstep(5) + L(10, 0) a2 = a1 | C(1, 5) b1 = a1 | R(5) b2 = a2 | R(5) t = np.linspace(-1, 10, 1000) fig = figure() ax = fig.add_subplot(111) # Open-circuit voltage across R ax.plot(t, b1.v.evaluate(t), linewidth=2, label='without C') ax.plot(t, b2.v.evaluate(t), linewidth=2, label='with C') ax.legend() ax.set_xlabel('Time (s)') ax.set_ylabel('Voltage (V)') ax.grid(True) fig = figure() ax = fig.add_subplot(111) # Short-circuit current through R ax.plot(t, b1.isc.evaluate(t), linewidth=2, label='without C') ax.plot(t, b2.isc.evaluate(t), linewidth=2, label='with C') ax.legend() ax.set_xlabel('Time (s)') ax.set_ylabel('Current (A)')
from lcapy import Vstep, R, L, C import numpy as np from matplotlib.pyplot import figure, savefig, show a = Vstep(5) + L(10) b = a | R(5) t = np.linspace(-1, 10, 1000) fig = figure() ax = fig.add_subplot(111) # Open-circuit voltage across R ax.plot(t, b.v.evaluate(t), linewidth=2) ax.set_xlabel('Time (s)') ax.set_ylabel('Voltage (V)') ax.grid(True) fig = figure() ax = fig.add_subplot(111) # Short-circuit current through R ax.plot(t, b.isc.evaluate(t), linewidth=2) ax.set_xlabel('Time (s)') ax.set_ylabel('Current (A)') ax.grid(True) show()
from lcapy import R, C n = C(2) | (R(3) + (C(4) | (R(5) + (C(6))))) n.draw(__file__.replace('.py', '.png'), form='ladder')
from lcapy import R, C N = (R(1) + ((R(2) + R(3)) | (C(6) + C(7) + C(8)))) | (R(4) + R(5)) N.draw(label_values=True)
from lcapy import Vstep, R, L, C, t from matplotlib.pyplot import savefig from numpy import linspace a = Vstep(10) + R(0.1) + C(0.4) + L(0.2, 0) vt = linspace(0, 10, 1000) a.Isc(t).plot(vt) savefig('series-VRLC1-isc.png')
from lcapy import R, C, L (R(2, color='blue') + C(3, color='green')).draw('colors.png')
def test_two_port(self): Rexe = 0.0000001 Rexc = 0.0000001 Rwe = 0.2545383947014702 Rwke = 0.7943030881649811 Rv = 8.852701208752846e-06 Rintere = 0.00034794562965549745 Rinterc = 0.00017397281482774872 Rwkc = 0.39715154408249054 Rwka = 744.3007160198263 Rwa = 456.90414284754644 Rwc = 0.1272691973507351 Rtota= R(Rexe) + (R(Rwa) | R(Rwe) + (R(Rwka)|R(Rwke)+R(Rintere)+R(Rv)+R(Rinterc)+R(Rwkc))+ R(Rwc))+ R(Rexc) # | | Rtot = R(Rexe) + (R(Rwa) | R(Rwe) + (R(Rwka)|R(Rwke)+R(Rintere)+R(Rv)+R(Rinterc)+R(Rwkc))+ R(Rwc)) + (R(1.6+Rexc) | (R(Rexe) + (R(Rwa) | R(Rwe) + (R(Rwka)|R(Rwke)+R(Rintere)+R(Rv)+R(Rinterc)+R(Rwkc))+ R(Rwc))+ R(Rexc))) print(Rtot.simplify()) Rtot.draw('test.pdf') Rtot_2 = LSection(Rtota,Rtota) ans1 = Rtot_2.Y1sc print(ans1.simplify()) ans2 = Rtot_2.Y2sc print(ans2.simplify())