def test_pulse(): """Test time_functions.pulse""" # as we cannot define a periodic function in sympy, we will only check one # period v1, v2, td, tr, pw, tf, per, time = sympy.symbols('v1, v2, td, tr, pw, ' + 'tf, per, time') F = v1*Heaviside(td - time) + \ ((v2 - v1)/tr*time + v1 +(v1 - v2)*td/tr)* \ Heaviside(time - td)*Heaviside(td + tr - time) + \ v2*Heaviside(time - td - tr)*Heaviside(td + tr + pw - time) + \ ((v1 - v2)/tf*time + v2 + (-v1*(pw + td + tr) + v2*(pw + td + tr))/tf)* \ Heaviside(time - td - tr - pw)*Heaviside(td + tr + pw + tf - time) + \ v1*Heaviside(time - td - tr - pw - tf) v1n, v2n, tdn, trn, pwn, tfn, pern = (-2, 3, 0.1e-3, 0.05e-3, .5e-3, .01e-3, 1e-3) f = time_functions.pulse(v1=v1n, v2=v2n, td=tdn, tr=trn, pw=pwn, tf=tfn, per=pern) FS = sympy.lambdify( time, sympy.N( F.subs( dict(v1=v1n, v2=v2n, td=tdn, tr=trn, pw=pwn, tf=tfn, per=pern)))) t = np.linspace(0, 1e-3, 1e3) for ti in t: assert np.allclose(f(ti), float(FS(ti)), rtol=1e-4)
def test_pulse(): """Test time_functions.pulse""" # as we cannot define a periodic function in sympy, we will only check one # period v1, v2, td, tr, pw, tf, per, time = sympy.symbols('v1, v2, td, tr, pw, ' + 'tf, per, time') F = v1*Heaviside(td - time) + \ ((v2 - v1)/tr*time + v1 +(v1 - v2)*td/tr)* \ Heaviside(time - td)*Heaviside(td + tr - time) + \ v2*Heaviside(time - td - tr)*Heaviside(td + tr + pw - time) + \ ((v1 - v2)/tf*time + v2 + (-v1*(pw + td + tr) + v2*(pw + td + tr))/tf)* \ Heaviside(time - td - tr - pw)*Heaviside(td + tr + pw + tf - time) + \ v1*Heaviside(time - td - tr - pw - tf) v1n, v2n, tdn, trn, pwn, tfn, pern = (-2, 3, 0.1e-3, 0.05e-3, .5e-3, .01e-3, 1e-3) f = time_functions.pulse( v1=v1n, v2=v2n, td=tdn, tr=trn, pw=pwn, tf=tfn, per=pern) FS = sympy.lambdify( time, sympy.N( F.subs( dict(v1=v1n, v2=v2n, td=tdn, tr=trn, pw=pwn, tf=tfn, per=pern)))) t = np.linspace(0, 1e-3, 1e3) for ti in t: assert np.allclose(f(ti), float(FS(ti)), rtol=1e-4)
def test(): """Test pulse and sin API""" step = time_functions.pulse(v1=0, v2=1, td=500e-9, tr=1e-12, pw=1, tf=1e-12, per=2) damped_sin = time_functions.sin(vo=0, va=1, td=500e-9, freq=15e3, theta=5e3, phi=90.) exp = time_functions.exp(v1=.5, v2=-.05, td1=0, tau1=20e-6, td2=400e-6, tau2=20e-6) mycircuit = circuit.Circuit(title="Butterworth Example circuit", filename=None) gnd = mycircuit.get_ground_node() mycircuit.add_resistor(part_id="R1", n1="n1", n2="n2", value=600) mycircuit.add_inductor(part_id="L1", n1="n2", n2="n3", value=15.24e-3) mycircuit.add_capacitor(part_id="C1", n1="n3", n2=gnd, value=119.37e-9) mycircuit.add_inductor(part_id="L2", n1="n3", n2="n4", value=61.86e-3) mycircuit.add_capacitor(part_id="C2", n1="n4", n2=gnd, value=155.12e-9) mycircuit.add_resistor(part_id="R2", n1="n4", n2=gnd, value=1.2e3) mycircuit.add_vsource("V1", n1="n1", n2='n5', dc_value=3.3333, ac_value=.33333, function=step) mycircuit.add_vsource("V2", n1="n5", n2='n6', dc_value=3.3333, ac_value=.33333, function=damped_sin) mycircuit.add_vsource("V3", n1="n6", n2=gnd, dc_value=3.3333, ac_value=.33333, function=exp) op_analysis = ahkab.new_op(outfile='time_functions') ac_analysis = ahkab.new_ac(start=1e3, stop=1e5, points=100, outfile='time_functions') tran_analysis = ahkab.new_tran(tstart=0, tstop=1.2e-3, tstep=1e-6, x0=None, outfile='time_functions') testbench = testing.APITest('time_functions', mycircuit, [op_analysis, ac_analysis, tran_analysis], skip_on_travis=True, er=1e-3, ea=1e-5) testbench.setUp() testbench.test() if cli: r = ahkab.run(mycircuit, an_list=[op_analysis, ac_analysis, tran_analysis]) fig = plt.figure() plt.title(mycircuit.title + " - TRAN Simulation") plt.plot(r['tran']['T'], r['tran']['VN1'], label="Input voltage") plt.hold(True) plt.plot(r['tran']['T'], r['tran']['VN4'], label="output voltage") plt.legend() plt.hold(False) plt.grid(True) #plt.ylim([0,1.2]) plt.ylabel('Step response') plt.xlabel('Time [s]') fig.savefig('tran_plot.png') fig = plt.figure() plt.subplot(211) plt.semilogx(r['ac']['w'], np.abs(r['ac']['Vn4']), 'o-') plt.ylabel('abs(V(n4)) [V]') plt.title(mycircuit.title + " - AC Simulation") plt.subplot(212) plt.grid(True) plt.semilogx(r['ac']['w'], np.angle(r['ac']['Vn4']), 'o-') plt.xlabel('Angular frequency [rad/s]') plt.ylabel('arg(V(n4)) [rad]') fig.savefig('ac_plot.png') else: testbench.tearDown()
import numpy as np import ahkab from ahkab import ahkab, circuit, time_functions mycircuit = circuit.Circuit(title="Butterworth Example circuit", filename=None) gnd = mycircuit.get_ground_node() mycircuit.add_resistor("R1", n1="n1", n2="n2", value=600) mycircuit.add_inductor("L1", n1="n2", n2="n3", value=15.24e-3) mycircuit.add_capacitor("C1", n1="n3", n2=gnd, value=119.37e-9) mycircuit.add_inductor("L2", n1="n3", n2="n4", value=61.86e-3) mycircuit.add_capacitor("C2", n1="n4", n2=gnd, value=155.12e-9) mycircuit.add_resistor("R2", n1="n4", n2=gnd, value=1.2e3) voltage_step = time_functions.pulse(v1=0, v2=1, td=500e-9, tr=1e-12, pw=1, tf=1e-12, per=2) mycircuit.add_vsource("V1", n1="n1", n2=gnd, dc_value=5, ac_value=1, function=voltage_step) print mycircuit op_analysis = ahkab.new_op() ac_analysis = ahkab.new_ac(start=1e3, stop=1e5, points=100) tran_analysis = ahkab.new_tran(tstart=0, tstop=1.2e-3, tstep=1e-6, x0=None) r = ahkab.run(mycircuit, an_list=[op_analysis, ac_analysis, tran_analysis]) import pylab fig = pylab.figure() pylab.title(mycircuit.title + " - TRAN Simulation") pylab.plot(r['tran']['T'], r['tran']['VN1'], label="Input voltage")
mycircuit = circuit.Circuit(title="Butterworth Example circuit", filename=None) gnd = mycircuit.get_ground_node() mycircuit.add_resistor("R1", n1="n1", n2="n2", value=600) mycircuit.add_inductor("L1", n1="n2", n2="n3", value=15.24e-3) mycircuit.add_capacitor("C1", n1="n3", n2=gnd, value=119.37e-9) mycircuit.add_inductor("L2", n1="n3", n2="n4", value=61.86e-3) mycircuit.add_capacitor("C2", n1="n4", n2=gnd, value=155.12e-9) mycircuit.add_resistor("R2", n1="n4", n2=gnd, value=1.2e3) voltage_step = time_functions.pulse(v1=0, v2=1, td=500e-9, tr=1e-12, pw=1, tf=1e-12, per=2) mycircuit.add_vsource("V1", n1="n1", n2=gnd, dc_value=5, ac_value=1, function=voltage_step) print mycircuit op_analysis = ahkab.new_op() ac_analysis = ahkab.new_ac(start=1e3, stop=1e5, points=100) tran_analysis = ahkab.new_tran(tstart=0, tstop=1.2e-3, tstep=1e-6, x0=None)
elif V_DC_pattern: V_Name = V_DC_pattern.group(1) V_Np = V_DC_pattern.group(2) V_Nn = V_DC_pattern.group(3) V_value = unit_transform(V_DC_pattern.group(7)) mycircuit.add_vsource(V_Name, n1=V_Np, n2=V_Nn, dc_value=V_value) elif V_AC_pattern: V_Name = V_AC_pattern.group(1) V_Np = V_AC_pattern.group(2) V_Nn = V_AC_pattern.group(3) V_value = unit_transform(V_AC_pattern.group(7)) V_AC_phase = V_AC_pattern.group(10) mycircuit.add_vsource(V_Name, n1=V_Np, n2=V_Nn, ac_value=V_value) elif V_PULSE_pattern: V_PULSE = PulseVoltageSource(line) v_pulse = time_functions.pulse(v1=V_PULSE.voltage_low, v2=V_PULSE.voltage_high, td=V_PULSE.delay, per=V_PULSE.period, tr=V_PULSE.rise, tf=V_PULSE.fall, pw=V_PULSE.width) mycircuit.add_vsource(V_PULSE.name, n1=V_PULSE.Np, n2=V_PULSE.Nn, function=v_pulse) elif I_DC_pattern: I_Name = I_DC_pattern.group(1) I_Np = I_DC_pattern.group(2) I_Nn = I_DC_pattern.group(3) I_value = unit_transform(I_DC_pattern.group(7)) mycircuit.add_isource(I_Name, n1=I_Np, n2=I_Nn, dc_value=I_value) elif I_AC_pattern: I_Name = I_AC_pattern.group(1) I_Np = I_AC_pattern.group(2) I_Nn = I_AC_pattern.group(3) I_value = unit_transform(I_AC_pattern.group(7)) mycircuit.add_isource(I_Name, n1=I_Np, n2=I_Nn, ac_value=I_value) elif E_pattern: E_name = E_pattern.group(1)
n2="n%s" % (3 * i + 2), value=Rs) mycircuit.add_capacitor("Cj%s" % (i), n1="n%s" % (3 * i + 2), n2=gnd, value=Cj) # mycircuit.add_resistor("Rsub%s" % (i), n1="n%s" %(4*i-1), n2="n%s" %(4*i), value=20000.28) # mycircuit.add_capacitor("Cox%s" % (i), n1="n%s" %(4*i), n2=gnd, value=4.09e-20) mycircuit.add_resistor("Rt", n1="n%s" % (3 * i + 1), n2=gnd, value=25.0) # voltage_step = time_functions.pulse(v1=0, v2=1, td=500e-9, tr=1e-12, pw=1, tf=1e-12, per=2) voltage_step = time_functions.pulse(v1=0, v2=1, td=0.1e-9, tr=1e-12, pw=0.5e-9, tf=1e-12, per=1) # voltage_step = time_functions.pulse(v1=0, v2=1, td=1e-12, tr=1e-15, pw=1, tf=1e-15, per=1) mycircuit.add_vsource("V1", n1="n1", n2=gnd, dc_value=0, ac_value=0.1, function=voltage_step) print mycircuit op_analysis = ahkab.new_op() ac_analysis = ahkab.new_ac(start=1e8, stop=5e10, points=1e2)