def test(): """Test NL AC analysis (API)""" cir = ahkab.Circuit('CS amplifier') mys = ahkab.time_functions.sin(0, 1, 60) cir.add_vsource('vin', '1', '0', dc_value=3, ac_value=1) cir.add_vsource('vdd', '3', '0', dc_value=30) cir.add_resistor('Rd', '3', '2', 10e3) cir.add_capacitor('Cd', '3', '2', 40e-12) cir.add_resistor('Rs', '4', '0', 1e3) cir.add_capacitor('Cs', '4', '0', 4e-6) cir.add_model('ekv', 'ekv0', {'TYPE': 'n', 'VTO': .4, 'KP': 1e-2}) cir.add_mos('m1', '2', '1', '4', '0', w=100e-6, l=1e-6, model_label='ekv0') print(cir) opa = ahkab.new_op(outfile='acnl', verbose=6) aca = ahkab.new_ac(1, 100e6, 10e3, outfile='acnl', verbose=6) r = ahkab.run(cir, [opa, aca])['ac'] testbench = testing.APITest('acnl', cir, [opa, aca], skip_on_travis=False, er=1e-3, ea=1e-5) testbench.setUp() testbench.test() if not cli: testbench.tearDown()
def test(): """Test pulse and sin API""" step = devices.pulse(v1=0, v2=1, td=500e-9, tr=1e-12, pw=1, tf=1e-12, per=2) damped_sin = devices.sin(vo=0, va=1, td=500e-9, freq=15e3, theta=5e3, phi=90.) exp = devices.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()
def test(): """Test CCCS API""" # The circuit is: #test for transresitances #va 1 2 type=vdc vdc=.1 vac=1 #r1 1 0 .5k #r2 2 0 .5k #h1 3 4 va 5000 #r3 3 0 1k #r4 4 5 1k #l1 5 0 10u #c1 5 0 10u #.op #.ac start=50k stop=5e5 nsteps=1000 #.symbolic #.plot ac |v(5)| mycircuit = circuit.Circuit(title="Test CCVS API", filename=None) gnd = mycircuit.get_ground_node() mycircuit.add_resistor(part_id="R1", n1="1", n2=gnd, value=500) mycircuit.add_resistor(part_id="R2", n1="2", n2=gnd, value=500) mycircuit.add_vsource("VA", n1="1", n2='2', dc_value=0.1, ac_value=1.) mycircuit.add_ccvs('H1', n1='3', n2='4', source_id='VA', value=5000) mycircuit.add_resistor(part_id="R3", n1="3", n2=gnd, value=1e3) mycircuit.add_resistor(part_id="R4", n1="4", n2="5", value=1e3) mycircuit.add_inductor(part_id="L1", n1="5", n2=gnd, value=10e-6) mycircuit.add_capacitor(part_id="C1", n1="5", n2=gnd, value=10e-6) print(mycircuit) op_analysis = ahkab.new_op(outfile='hvsource_api', verbose=6) symb_analysis = ahkab.new_symbolic(outfile='hvsource_api', verbose=6) ac_analysis = ahkab.new_ac(outfile='hvsource_api', start=7957.747, stop=79577.471, points=1000, verbose=6) testbench = testing.APITest('hvsource', mycircuit, [op_analysis, symb_analysis, ac_analysis], skip_on_travis=False, er=1e-3, ea=1e-5) testbench.setUp() testbench.test() if not cli: testbench.tearDown()
def test(): """Test CCCS API""" # The circuit is: # test for transconductors # va 1 2 type=vdc vdc=.1 # r1 1 0 .5k # r2 2 0 .5k # f1 3 4 va 5 # r3 3 0 1k # r4 4 0 1k # .op # .symbolic mycircuit = circuit.Circuit(title="Test CCCS API", filename=None) gnd = mycircuit.get_ground_node() mycircuit.add_resistor(part_id="R1", n1="1", n2=gnd, value=500) mycircuit.add_resistor(part_id="R2", n1="2", n2=gnd, value=500) mycircuit.add_cccs('F1', n1='3', n2='4', source_id='VA', value=5) mycircuit.add_resistor(part_id="R3", n1="3", n2=gnd, value=1e3) mycircuit.add_resistor(part_id="R4", n1="4", n2=gnd, value=1e3) mycircuit.add_vsource("VA", n1="1", n2='2', dc_value=0.1) print(mycircuit) op_analysis = ahkab.new_op(outfile='fisource_api', verbose=6) symb_analysis = ahkab.new_symbolic(outfile='fisource_api', verbose=6) testbench = testing.APITest('fisource', mycircuit, [op_analysis, symb_analysis], skip_on_travis=False, er=1e-3, ea=1e-5) testbench.setUp() testbench.test() if not cli: testbench.tearDown()
def test(): """Test SVF Biquad""" mycircuit = Circuit(title="state variable filter") gnd = mycircuit.get_ground_node() buildsvf(mycircuit) mycircuit.add_vsource(part_id="V1", n1="in", n2=gnd, dc_value=5, ac_value=1) if cli: print(mycircuit) subs = {'E2':'E1', 'E3':'E1', 'R01':'R00', 'R02':'R00', 'R11':'R00', 'R10':'R00', 'C11':'C10', 'Rf2':'Rf1', 'Rin':'R00'} symbolic_sim = ahkab.new_symbolic(ac_enable=True, subs=subs, outfile='svf_biquad') ac_sim = ahkab.new_ac(start=0.1, stop=100e6, points=1000, x0=None, outfile='svf_biquad') testbench = testing.APITest('svf_biquad', mycircuit, [symbolic_sim, ac_sim], skip_on_travis=True, er=1e-3, ea=1e-5) testbench.setUp() testbench.test() if cli: r = ahkab.run(mycircuit, [symbolic_sim, ac_sim]) E = r['symbolic'][0].as_symbol('E1') out_hp = sympy.limit(r['symbolic'][0]['VU1o'], E, sympy.oo, '+') out_bp = sympy.limit(r['symbolic'][0]['VU2o'], E, sympy.oo, '+') out_lp = sympy.limit(r['symbolic'][0]['VU3o'], E, sympy.oo, '+') out_hp = out_hp.simplify() out_bp = out_bp.simplify() out_lp = out_lp.simplify() print("VU1o =", out_hp) print("VU2o =", out_bp) print("VU3o =", out_lp) w = sympy.Symbol('w') out_hp = out_hp.subs({r['symbolic'][0].as_symbol('RF1'):10e3, r['symbolic'][0].as_symbol('C10'):15e-9, r['symbolic'][0].as_symbol('V1'):1, r['symbolic'][0].as_symbol('s'):1j*w, }) out_bp = out_bp.subs({r['symbolic'][0].as_symbol('RF1'):10e3, r['symbolic'][0].as_symbol('C10'):15e-9, r['symbolic'][0].as_symbol('V1'):1, r['symbolic'][0].as_symbol('s'):1j*w, }) out_lp = out_lp.subs({r['symbolic'][0].as_symbol('RF1'):10e3, r['symbolic'][0].as_symbol('C10'):15e-9, r['symbolic'][0].as_symbol('V1'):1, r['symbolic'][0].as_symbol('s'):1j*w, }) out_lp = sympy.lambdify((w,), out_lp) out_bp = sympy.lambdify((w,), out_bp) out_hp = sympy.lambdify((w,), out_hp) ws = r['ac']['w'][::30] fig = plt.figure() plt.title(mycircuit.title) plt.subplot(211) plt.hold(True) plt.semilogx(r['ac']['w']/2./np.pi, 20*np.log10(np.abs(r['ac']['VU1o'])), label="HP output (AC)") plt.semilogx(r['ac']['w']/2./np.pi, 20*np.log10(np.abs(r['ac']['VU2o'])), label="BP output (AC)") plt.semilogx(r['ac']['w']/2./np.pi, 20*np.log10(np.abs(r['ac']['VU3o'])), label="LP output (AC)") plt.semilogx(ws/2./np.pi, 20*np.log10(np.abs(out_hp(ws))), 'v', label="HP output (SYMB)") plt.semilogx(ws/2./np.pi, 20*np.log10(np.abs(out_bp(ws))), 'v', label="BP output (SYMB)") plt.semilogx(ws/2./np.pi, 20*np.log10(np.abs(out_lp(ws))), 'v', label="LP output (SYMB)") plt.hold(False) plt.grid(True) plt.legend() plt.ylabel('Magnitude [dB]') plt.xlabel('Frequency [Hz]') plt.subplot(212) plt.hold(True) plt.semilogx(r['ac']['w']/2./np.pi, np.angle(r['ac']['VU1o']), label="HP output (AC)") plt.semilogx(r['ac']['w']/2./np.pi, np.angle(r['ac']['VU2o']), label="BP output (AC)") plt.semilogx(r['ac']['w']/2./np.pi, np.angle(r['ac']['VU3o']), label="LP output (AC)") plt.semilogx(ws/2./np.pi, np.angle(out_hp(ws)), 'v', label="HP output (SYMB)") plt.semilogx(ws/2./np.pi, np.angle(out_bp(ws)), 'v', label="BP output (SYMB)") plt.semilogx(ws/2./np.pi, np.angle(out_lp(ws)), 'v', label="LP output (SYMB)") plt.legend() plt.hold(False) plt.grid(True) #plt.ylim([0,1.2]) plt.ylabel('Phase [rad]') plt.xlabel('Frequency [Hz]') fig.savefig('ac_plot.png') else: testbench.tearDown()
def test(): """Full wave rectifier test circuit""" cir = assemble() ## define analyses op1 = ahkab.new_op(outfile='rectifier') tran1 = ahkab.new_tran(0, 200e-3, 1e-4, outfile='rectifier', verbose=0 + cli * 6) # set the options sim_opts = {} sim_opts.update({'gmin': 1e-7}) sim_opts.update({'nl_voltages_lock': False}) sim_opts.update({'nl_voltages_lock_factor': 20}) sim_opts.update({'iea': 1e-1}) sim_opts.update({'default_tran_method': 'TRAP'}) sim_opts.update({'hmin': 1e-20}) sim_opts.update({'transient_max_nr_iter': 200}) ## create a testbench testbench = testing.APITest('rectifier', cir, [op1, tran1], skip_on_travis=True, sim_opts=sim_opts, ea=1e-1, er=1.) ## setup and test testbench.setUp() testbench.test() ## this section is recommended. If something goes wrong, you may call the ## test from the cli and the plots to video in the following will allow ## for quick inspection if cli: ## re-run the test to grab the results cir = assemble() res = ahkab.run(cir, an_list=[op1, tran1]) # print-out for good measure print("OP Results:") print(list(res['op'].items())) ## plot and save interesting data fig = plt.figure() plt.title(cir.title + " inputs") plt.plot(res['tran'].get_x(), res['tran']['VINA'] - res['tran']['VINB'], label='Transf. input') plt.hold(True) plt.plot(res['tran'].get_x(), res['tran']['vint1'], label='Transformer output #1') plt.plot(res['tran'].get_x(), res['tran']['vint2'], label='Transformer output #2') plt.hold(False) plt.grid(True) plt.legend() plt.ylabel('Voltage [V]') plt.xlabel('Time [s]') fig.savefig('rectf1_plot.png') fig = plt.figure() plt.title(cir.title + " outputs") plt.plot(res['tran'].get_x(), res['tran']['vint4'] - res['tran']['vint3'], label="output voltage") plt.legend() plt.grid(True) plt.ylabel('Voltage [V]') plt.xlabel('Time [s]') fig.savefig('rectf2_plot.png') else: testbench.tearDown()